Skip to content

Commit 284cd84

Browse files
committed
Un-revert the CF portions of swiftlang#2764
See <swiftlang#2764>.
1 parent 659b9b1 commit 284cd84

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,8 +1460,22 @@ CF_PRIVATE int asprintf(char **ret, const char *format, ...) {
14601460
extern void swift_retain(void *);
14611461
extern void swift_release(void *);
14621462

1463+
#if TARGET_OS_WIN32
1464+
typedef struct _CFThreadSpecificData {
1465+
CFTypeRef value;
1466+
_CFThreadSpecificKey key;
1467+
} _CFThreadSpecificData;
1468+
#endif
1469+
14631470
static void _CFThreadSpecificDestructor(void *ctx) {
1471+
#if TARGET_OS_WIN32
1472+
_CFThreadSpecificData *data = (_CFThreadSpecificData *)ctx;
1473+
FlsSetValue(data->key, NULL);
1474+
swift_release(data->value);
1475+
free(data);
1476+
#else
14641477
swift_release(ctx);
1478+
#endif
14651479
}
14661480

14671481
_CFThreadSpecificKey _CFThreadSpecificKeyCreate() {
@@ -1476,18 +1490,36 @@ _CFThreadSpecificKey _CFThreadSpecificKeyCreate() {
14761490

14771491
CFTypeRef _Nullable _CFThreadSpecificGet(_CFThreadSpecificKey key) {
14781492
#if TARGET_OS_WIN32
1479-
return (CFTypeRef)FlsGetValue(key);
1493+
_CFThreadSpecificData *data = (_CFThreadSpecificData *)FlsGetValue(key);
1494+
if (data == NULL) {
1495+
return NULL;
1496+
}
1497+
return data->value;
14801498
#else
14811499
return (CFTypeRef)pthread_getspecific(key);
14821500
#endif
14831501
}
14841502

14851503
void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value) {
1504+
// Intentionally not calling `swift_release` for previous value.
1505+
// OperationQueue uses these API (through NSThreadSpecific), and balances
1506+
// retain count manually.
14861507
#if TARGET_OS_WIN32
1508+
free(FlsGetValue(key));
1509+
1510+
_CFThreadSpecificData *data = NULL;
14871511
if (value != NULL) {
1512+
data = malloc(sizeof(_CFThreadSpecificData));
1513+
if (!data) {
1514+
HALT_MSG("Out of memory");
1515+
}
1516+
data->value = value;
1517+
data->key = key;
1518+
14881519
swift_retain((void *)value);
14891520
}
1490-
FlsSetValue(key, value);
1521+
1522+
FlsSetValue(key, data);
14911523
#else
14921524
if (value != NULL) {
14931525
swift_retain((void *)value);

0 commit comments

Comments
 (0)