ObjFW  Check-in [49aee5736e]

Overview
Comment:tlskey.m: Use hashtable from runtime on AmigaOS

tlskey.m used OFMapTable only on AmigaOS. This became a problem when
autorelease pools were moved into the runtime, as autorelease pools use
TLS. The build then broke, as there suddenly was a dependency from the
runtime on ObjFW.

This now uses the hashtable from the runtime and also no longer uses
OFList, thus fixing the build. As we always use the runtime on AmigaOS
anyway, this is fine.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 49aee5736ee7ec8d9911da074ec2930454d24bddf8961260d7c6594a139ce8b9
User & Date: js on 2020-01-25 20:04:54
Other Links: manifest | tags
Context
2020-02-16
18:22
OF{LHA,Tar,ZIP}Archive: Gracefully handle close check-in: 1d6d3eb61c user: js tags: trunk
2020-01-25
20:04
tlskey.m: Use hashtable from runtime on AmigaOS check-in: 49aee5736e user: js tags: trunk
2020-01-24
03:03
runtime: Correctly handle AR pool push during pop check-in: e7f4f80e23 user: js tags: trunk
Changes

Modified src/runtime/amiga-glue.m from [67a0ee932e] to [5dde941d59].

745
746
747
748
749
750
751


















































745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
id __saveds
glue__objc_rootAutorelease PPC_PARAMS(id object)
{
	M68K_ARG(id, object, a0)

	return _objc_rootAutorelease(object);
}

struct objc_hashtable *__saveds
glue_objc_hashtable_new PPC_PARAMS(objc_hashtable_hash_func hash,
    objc_hashtable_equal_func equal, uint32_t size)
{
	M68K_ARG(objc_hashtable_hash_func, hash, a0)
	M68K_ARG(objc_hashtable_equal_func, equal, a1)
	M68K_ARG(uint32_t, size, d0)

	return objc_hashtable_new(hash, equal, size);
}

void __saveds
glue_objc_hashtable_set PPC_PARAMS(struct objc_hashtable *table,
    const void *key, const void *object)
{
	M68K_ARG(struct objc_hashtable *, table, a0)
	M68K_ARG(const void *, key, a1)
	M68K_ARG(const void *, object, a2)

	objc_hashtable_set(table, key, object);
}

void *__saveds
glue_objc_hashtable_get PPC_PARAMS(struct objc_hashtable *table,
    const void *key)
{
	M68K_ARG(struct objc_hashtable *, table, a0)
	M68K_ARG(const void *, key, a1)

	return objc_hashtable_get(table, key);
}

void __saveds
glue_objc_hashtable_delete PPC_PARAMS(struct objc_hashtable *table,
    const void *key)
{
	M68K_ARG(struct objc_hashtable *, table, a0)
	M68K_ARG(const void *, key, a1)

	objc_hashtable_delete(table, key);
}

void __saveds
glue_objc_hashtable_free PPC_PARAMS(struct objc_hashtable *table)
{
	M68K_ARG(struct objc_hashtable *, table, a0)

	objc_hashtable_free(table);
}

Modified src/runtime/amiga-library.m from [e964a079fa] to [3100a40497].

138
139
140
141
142
143
144





145
146
147
148
149
150
151
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156







+
+
+
+
+







extern objc_property_t *glue_class_copyPropertyList(void);
extern const char *glue_property_getName(void);
extern char *glue_property_copyAttributeValue(void);
extern void *glue_objc_destructInstance(void);
extern void *glue_objc_autoreleasePoolPush(void);
extern void glue_objc_autoreleasePoolPop(void);
extern id glue__objc_rootAutorelease(void);
extern struct objc_hashtable *glue_objc_hashtable_new(void);
extern void glue_objc_hashtable_set(void);
extern void *glue_objc_hashtable_get(void);
extern void glue_objc_hashtable_delete(void);
extern void glue_objc_hashtable_free(void);

#ifdef OF_MORPHOS
const ULONG __abox__ = 1;
#endif
struct ExecBase *SysBase;
struct objc_libc libc;
FILE *stdout;
659
660
661
662
663
664
665





666
667
668
669
670
671
672
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682







+
+
+
+
+







	(CONST_APTR)glue_class_copyPropertyList,
	(CONST_APTR)glue_property_getName,
	(CONST_APTR)glue_property_copyAttributeValue,
	(CONST_APTR)glue_objc_destructInstance,
	(CONST_APTR)glue_objc_autoreleasePoolPush,
	(CONST_APTR)glue_objc_autoreleasePoolPop,
	(CONST_APTR)glue__objc_rootAutorelease,
	(CONST_APTR)glue_objc_hashtable_new,
	(CONST_APTR)glue_objc_hashtable_set,
	(CONST_APTR)glue_objc_hashtable_get,
	(CONST_APTR)glue_objc_hashtable_delete,
	(CONST_APTR)glue_objc_hashtable_free,
	(CONST_APTR)-1,
#ifdef OF_MORPHOS
	(CONST_APTR)FUNCARRAY_END
#endif
};
#pragma GCC diagnostic pop

Modified src/runtime/amigaos3.sfd from [c02a06707d] to [c645261c7f].

1
2
3
4
5
6

7
8
9
10
11
12
13
14
15
16
1
2
3
4
5

6
7


8
9
10
11
12
13
14





-
+

-
-







==base _ObjFWRTBase
==basetype struct Library *
==libname objfwrt68k.library
==bias 30
==public
* Functions that are only for the linklib.
* The following function is only for the linklib.
bool glue_objc_init(unsigned int version, struct objc_libc *libc, FILE *stdout, FILE *stderr)(d0,a0,a1,a2)
* These have a built-in declaration in the compiler that does not use the
* registers and thus always need glue.
void glue___objc_exec_class(struct objc_module *_Nonnull module)(a0)
IMP _Nonnull glue_objc_msg_lookup(id _Nullable object, SEL _Nonnull selector)(a0,a1)
IMP _Nonnull glue_objc_msg_lookup_stret(id _Nullable object, SEL _Nonnull selector)(a0,a1)
IMP _Nonnull glue_objc_msg_lookup_super(struct objc_super *_Nonnull super, SEL _Nonnull selector)(a0,a1)
IMP _Nonnull glue_objc_msg_lookup_super_stret(struct objc_super *_Nonnull super, SEL _Nonnull selector)(a0,a1)
Class _Nullable glue_objc_lookUpClass(const char *_Nonnull name)(a0)
Class _Nullable glue_objc_getClass(const char *_Nonnull name)(a0)
81
82
83
84
85
86
87






88
79
80
81
82
83
84
85
86
87
88
89
90
91
92







+
+
+
+
+
+

objc_property_t _Nullable *_Nullable glue_class_copyPropertyList(Class _Nullable class_, unsigned int *_Nullable outCount)(a0,a1)
const char *_Nonnull glue_property_getName(objc_property_t _Nonnull property)(a0)
char *_Nullable glue_property_copyAttributeValue(objc_property_t _Nonnull property, const char *_Nonnull name)(a0,a1)
void *_Nullable glue_objc_destructInstance(id _Nullable object)(a0)
void *_Null_unspecified glue_objc_autoreleasePoolPush(void)()
void glue_objc_autoreleasePoolPop(void *_Null_unspecified pool)(a0)
id _Nullable glue__objc_rootAutorelease(id _Nullable object)(a0)
* The following functions are private! Don't use!
struct objc_hashtable *_Nonnull glue_objc_hashtable_new(objc_hashtable_hash_func hash, objc_hashtable_equal_func equal, uint32_t size)(a0,a1,d0)
void glue_objc_hashtable_set(struct objc_hashtable *_Nonnull table, const void *_Nonnull key, const void *_Nonnull object)(a0,a1,a2)
void *_Nullable glue_objc_hashtable_get(struct objc_hashtable *_Nonnull table, const void *_Nonnull key)(a0,a1)
void glue_objc_hashtable_delete(struct objc_hashtable *_Nonnull table, const void *_Nonnull key)(a0,a1)
void glue_objc_hashtable_free(struct objc_hashtable *_Nonnull table)(a0)
==end

Modified src/runtime/linklib/linklib.m from [af59ade95f] to [570378ad75].

671
672
673
674
675
676
677
































671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
}

id
_objc_rootAutorelease(id object)
{
	return glue__objc_rootAutorelease(object);
}

struct objc_hashtable *
objc_hashtable_new(objc_hashtable_hash_func hash,
    objc_hashtable_equal_func equal, uint32_t size)
{
	return glue_objc_hashtable_new(hash, equal, size);
}

void
objc_hashtable_set(struct objc_hashtable *table, const void *key,
    const void *object)
{
	glue_objc_hashtable_set(table, key, object);
}

void *
objc_hashtable_get(struct objc_hashtable *table, const void *key)
{
	return glue_objc_hashtable_get(table, key);
}

void
objc_hashtable_delete(struct objc_hashtable *table, const void *key)
{
	glue_objc_hashtable_delete(table, key);
}

void
objc_hashtable_free(struct objc_hashtable *table)
{
	glue_objc_hashtable_free(table);
}

Modified src/runtime/morphos-clib.h from [779ee8fb5d] to [cfb2b6af7e].

1

2
3
4
5
6
7
8
9
10

1
2

3
4
5
6
7
8
9
-
+

-







/* Functions that are only for the linklib. */
/* The following function is only for the linklib. */
bool glue_objc_init(unsigned int, struct objc_libc *, FILE *, FILE *);
/* All other functions. */
void glue___objc_exec_class(struct objc_module *);
IMP glue_objc_msg_lookup(id, SEL);
IMP glue_objc_msg_lookup_stret(id, SEL);
IMP glue_objc_msg_lookup_super(struct objc_super *, SEL);
IMP glue_objc_msg_lookup_super_stret(struct objc_super *, SEL);
Class glue_objc_lookUpClass(const char *);
Class glue_objc_getClass(const char *);
75
76
77
78
79
80
81






74
75
76
77
78
79
80
81
82
83
84
85
86







+
+
+
+
+
+
objc_property_t *glue_class_copyPropertyList(Class, unsigned int *);
const char *glue_property_getName(objc_property_t);
char *glue_property_copyAttributeValue(objc_property_t, const char *);
void *glue_objc_destructInstance(id);
void *glue_objc_autoreleasePoolPush(void);
void glue_objc_autoreleasePoolPop(void *);
id glue__objc_rootAutorelease(id);
/* The following functions are private! Don't use! */
struct objc_hashtable *glue_objc_hashtable_new(objc_hashtable_hash_func hash, objc_hashtable_equal_func equal, uint32_t size);
void glue_objc_hashtable_set(struct objc_hashtable *table, const void *key, const void *object);
void *glue_objc_hashtable_get(struct objc_hashtable *table, const void *key);
void glue_objc_hashtable_delete(struct objc_hashtable *table, const void *key);
void glue_objc_hashtable_free(struct objc_hashtable *table);

Modified src/runtime/morphos.fd from [5afb6ca118] to [f7d065769a].

1
2
3
4

5
6
7
8
9
10
11
1
2
3

4
5
6
7
8
9
10
11



-
+







##base _ObjFWRTBase
##bias 30
##public
* Functions that are only for the linklib.
* The following function is only for the linklib.
glue_objc_init(version,libc,stdout,stderr)(sysv,r12base)
glue___objc_exec_class(module)(sysv,r12base)
glue_objc_msg_lookup(object,selector)(sysv,r12base)
glue_objc_msg_lookup_stret(object,selector)(sysv,r12base)
glue_objc_msg_lookup_super(super,selector)(sysv,r12base)
glue_objc_msg_lookup_super_stret(super,selector)(sysv,r12base)
glue_objc_lookUpClass(name)(sysv,r12base)
77
78
79
80
81
82
83






84
77
78
79
80
81
82
83
84
85
86
87
88
89
90







+
+
+
+
+
+

glue_class_copyPropertyList(class_,outCount)(sysv,r12base)
glue_property_getName(property)(sysv,r12base)
glue_property_copyAttributeValue(property,name)(sysv,r12base)
glue_objc_destructInstance(object)(sysv,r12base)
glue_objc_autoreleasePoolPush()(sysv,r12base)
glue_objc_autoreleasePoolPop(pool)(sysv,r12base)
glue__objc_rootAutorelease(object)(sysv,r12base)
* The following functions are private! Don't use!
glue_objc_hashtable_new(hash,equal,size)(sysv,r12base)
glue_objc_hashtable_set(table,key,object)(sysv,r12base)
glue_objc_hashtable_get(table,key)(sysv,r12base)
glue_objc_hashtable_delete(table,key)(sysv,r12base)
glue_objc_hashtable_free(table)(sysv,r12base)
##end

Modified src/runtime/private.h from [a228e75679] to [82de8c2339].

25
26
27
28
29
30
31




32
33
34
35
36
37
38
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42







+
+
+
+







#  define _Nonnull
# endif
# ifndef _Nullable
#  define _Nullable
# endif
#endif

typedef uint32_t (*_Nonnull objc_hashtable_hash_func)(const void *_Nonnull key);
typedef bool (*_Nonnull objc_hashtable_equal_func)(const void *_Nonnull key1,
    const void *_Nonnull key2);

struct objc_class {
	Class _Nonnull isa;
	Class _Nullable superclass;
	const char *_Nonnull name;
	unsigned long version;
	unsigned long info;
	long instanceSize;
185
186
187
188
189
190
191
192
193


194
195
196
197
198
199
200
201
189
190
191
192
193
194
195


196
197

198
199
200
201
202
203
204







-
-
+
+
-








struct objc_hashtable_bucket {
	const void *_Nonnull key, *_Nonnull object;
	uint32_t hash;
};

struct objc_hashtable {
	uint32_t (*_Nonnull hash)(const void *_Nonnull key);
	bool (*_Nonnull equal)(const void *_Nonnull key1,
	objc_hashtable_hash_func hash;
	objc_hashtable_equal_func equal;
	    const void *_Nonnull key2);
	uint32_t count, size;
	struct objc_hashtable_bucket *_Nonnull *_Nullable data;
};

struct objc_sparsearray {
	struct objc_sparsearray_data {
		void *_Nullable next[256];
274
275
276
277
278
279
280
281
282

283
284
285
286
287
288
289
277
278
279
280
281
282
283


284
285
286
287
288
289
290
291







-
-
+







extern void objc_register_all_classes(struct objc_symtab *_Nonnull);
extern Class _Nullable objc_classname_to_class(const char *_Nonnull, bool);
extern void objc_unregister_class(Class _Nonnull);
extern void objc_unregister_all_classes(void);
extern uint32_t objc_hash_string(const void *_Nonnull);
extern bool objc_equal_string(const void *_Nonnull, const void *_Nonnull);
extern struct objc_hashtable *_Nonnull objc_hashtable_new(
    uint32_t (*_Nonnull)(const void *_Nonnull),
    bool (*_Nonnull)(const void *_Nonnull, const void *_Nonnull), uint32_t);
    objc_hashtable_hash_func, objc_hashtable_equal_func, uint32_t);
extern struct objc_hashtable_bucket objc_deleted_bucket;
extern void objc_hashtable_set(struct objc_hashtable *_Nonnull,
    const void *_Nonnull, const void *_Nonnull);
extern void *_Nullable objc_hashtable_get(struct objc_hashtable *_Nonnull,
    const void *_Nonnull);
extern void objc_hashtable_delete(struct objc_hashtable *_Nonnull,
    const void *_Nonnull);

Modified src/tlskey.h from [ae536e54d4] to [32bd25a13e].

29
30
31
32
33
34
35
36
37
38
39
40



41
42
43
44
45
46
47
29
30
31
32
33
34
35





36
37
38
39
40
41
42
43
44
45







-
-
-
-
-
+
+
+







#if defined(OF_HAVE_PTHREADS)
# include <pthread.h>
typedef pthread_key_t of_tlskey_t;
#elif defined(OF_WINDOWS)
# include <windows.h>
typedef DWORD of_tlskey_t;
#elif defined(OF_AMIGAOS)
# import "OFList.h"
@class OFMapTable;
typedef struct {
	OFMapTable *mapTable;
	of_list_object_t *listObject;
typedef struct of_tlskey {
	struct objc_hashtable *table;
	struct of_tlskey *next, *previous;
} *of_tlskey_t;
#endif

#ifdef __cplusplus
extern "C" {
#endif
extern bool of_tlskey_new(of_tlskey_t *key);

Modified src/tlskey.m from [397d0371d2] to [5550ceb6fb].

19
20
21
22
23
24
25
26
27





28
29
30

31












32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

48
49

50
51
52
53
54


















55
56
57
58
59
60
61
62
63
64
65
66
67
68




69
70







71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101


102
103

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119


120
121
122

123
124
125

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144




145
146
147
148
149
19
20
21
22
23
24
25


26
27
28
29
30
31


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

61
62
63
64





65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117












118
119
120
121
122
123
124


125
126
127

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142


143
144
145
146

147
148


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165



166
167
168
169
170
171
172
173
174







-
-
+
+
+
+
+

-
-
+

+
+
+
+
+
+
+
+
+
+
+
+















-
+


+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+














+
+
+
+
-
-
+
+
+
+
+
+
+










-
-
-
-
-
-
-
-
-
-
-
-







-
-
+
+

-
+














-
-
+
+


-
+

-
-
+
















-
-
-
+
+
+
+






#import "tlskey.h"

#ifdef OF_AMIGAOS
# include <exec/semaphores.h>
# include <proto/exec.h>

# import "OFMapTable.h"
# import "OFList.h"
/*
 * As we use this file in both the runtime and ObjFW, and since AmigaOS always
 * has the runtime, use the hashtable from the runtime.
 */
# import "runtime/private.h"

static const of_map_table_functions_t functions = { NULL };
static OFList *allKeys = nil;
static of_tlskey_t firstKey = NULL, lastKey = NULL;
static struct SignalSemaphore semaphore;

static uint32_t
hashFunc(const void *ptr)
{
	return (uint32_t)(uintptr_t)ptr;
}

static bool
equalFunc(const void *ptr1, const void *ptr2)
{
	return (ptr1 == ptr2);
}

OF_CONSTRUCTOR()
{
	InitSemaphore(&semaphore);
}
#endif

bool
of_tlskey_new(of_tlskey_t *key)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_key_create(key, NULL) == 0);
#elif defined(OF_WINDOWS)
	return ((*key = TlsAlloc()) != TLS_OUT_OF_INDEXES);
#elif defined(OF_AMIGAOS)
	if ((*key = calloc(1, sizeof(**key))) == NULL)
	if ((*key = malloc(sizeof(**key))) == NULL)
		return false;

	(*key)->table = NULL;
	/*
	 * We create the map table lazily, as some TLS are created in
	 * constructors, at which time OFMapTable is not available yet.
	 */


	ObtainSemaphore(&semaphore);
	@try {
		(*key)->next = NULL;
		(*key)->previous = lastKey;

		if (lastKey != NULL)
			lastKey->next = *key;

		lastKey = *key;

		if (firstKey == NULL)
			firstKey = *key;
	} @finally {
		ReleaseSemaphore(&semaphore);
	}

	/* We create the hash table lazily. */
	return true;
#endif
}

bool
of_tlskey_free(of_tlskey_t key)
{
#if defined(OF_HAVE_PTHREADS)
	return (pthread_key_delete(key) == 0);
#elif defined(OF_WINDOWS)
	return TlsFree(key);
#elif defined(OF_AMIGAOS)
	ObtainSemaphore(&semaphore);
	@try {
		if (key->previous != NULL)
			key->previous->next = key->next;
		if (key->next != NULL)
			key->next->previous = key->previous;
		[allKeys removeListObject: key->listObject];
		[key->mapTable release];

		if (firstKey == key)
			firstKey = key->next;
		if (lastKey == key)
			lastKey = key->previous;

		objc_hashtable_free(key->table);
		free(key);
	} @finally {
		ReleaseSemaphore(&semaphore);
	}

	return true;
#endif
}

#ifdef OF_AMIGAOS
static void
unsafeCreateMapTable(of_tlskey_t key)
{
	key->mapTable = [[OFMapTable alloc] initWithKeyFunctions: functions
						 objectFunctions: functions];

	if (allKeys == nil)
		allKeys = [[OFList alloc] init];

	key->listObject = [allKeys appendObject: key->mapTable];
}

void *
of_tlskey_get(of_tlskey_t key)
{
	void *ret;

	ObtainSemaphore(&semaphore);
	@try {
		if (key->mapTable == NULL)
			unsafeCreateMapTable(key);
		if (key->table == NULL)
			return NULL;

		ret = [key->mapTable objectForKey: FindTask(NULL)];
		ret = objc_hashtable_get(key->table, FindTask(NULL));
	} @finally {
		ReleaseSemaphore(&semaphore);
	}

	return ret;
}

bool
of_tlskey_set(of_tlskey_t key, void *ptr)
{
	ObtainSemaphore(&semaphore);
	@try {
		struct Task *task = FindTask(NULL);

		if (key->mapTable == NULL)
			unsafeCreateMapTable(key);
		if (key->table == NULL)
			key->table = objc_hashtable_new(hashFunc, equalFunc, 2);

		if (ptr == NULL)
			[key->mapTable removeObjectForKey: task];
			objc_hashtable_delete(key->table, task);
		else
			[key->mapTable setObject: ptr
					  forKey: task];
			objc_hashtable_set(key->table, task, ptr);
	} @catch (id e) {
		return false;
	} @finally {
		ReleaseSemaphore(&semaphore);
	}

	return true;
}

void
of_tlskey_thread_exited(void)
{
	ObtainSemaphore(&semaphore);
	@try {
		struct Task *task = FindTask(NULL);

		for (of_list_object_t *iter = allKeys.firstListObject;
		    iter != NULL; iter = iter->next)
			[iter->object removeObjectForKey: task];
		for (of_tlskey_t iter = firstKey; iter != NULL;
		    iter = iter->next)
			if (iter->table != NULL)
				objc_hashtable_delete(iter->table, task);
	} @finally {
		ReleaseSemaphore(&semaphore);
	}
}
#endif