Ticket #182: key_destructor.diff
File key_destructor.diff, 2.7 KB (added by , 7 years ago) |
---|
-
my_os2thread.c
190 190 pthread_setspecific(THR_self, NULL); 191 191 } 192 192 193 // call available destructor associated with key 194 pthread_key_destructor(); 195 193 196 // thread is joinable, pthread_join is supposed to be called from main thread 194 197 195 198 -
my_os2key.c
23 23 #define INCL_EXAPIS_MAPPINGS 24 24 #include <os2emx.h> 25 25 26 #include <stdlib.h> 27 26 28 #include "pthread.h" 27 29 #include "tls.h" 28 30 31 struct pthread_key_pair_t_ 32 { 33 pthread_key_t key; 34 void (*destructor)(void*); 35 struct pthread_key_pair_t_ *next; 36 }; 37 38 typedef struct pthread_key_pair_t_ * pthread_key_pair_t; 39 40 static pthread_key_pair_t pair_start = NULL; 41 29 42 int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)) 30 43 { 44 pthread_key_pair_t new_pair; 31 45 46 new_pair = calloc(1, sizeof(*new_pair)); 47 if (new_pair == NULL) { 48 errno = ENOMEM; 49 return -1; 50 } 51 32 52 if ((*(ULONG *)key = TlsAlloc()) != -1) 53 { 54 new_pair->key = *key; 55 new_pair->destructor = destructor; 56 new_pair->next = pair_start; 57 58 pair_start = new_pair; 59 33 60 return 0; 61 } 62 63 free(new_pair); 64 34 65 errno = EAGAIN; 35 66 return -1; 36 67 } 37 68 38 69 int pthread_key_delete(pthread_key_t key) 39 70 { 71 pthread_key_pair_t prev = NULL; 72 pthread_key_pair_t pair; 73 74 for (pair = pair_start; pair != NULL; pair = pair->next) { 75 if (pair->key == key) { 76 if (pair == pair_start) 77 pair_start = pair->next; 78 else 79 prev->next = pair->next; 80 81 free(pair); 82 break; 83 } 84 85 prev = pair; 86 } 87 40 88 if (TlsFree((ULONG)key)) 41 89 return 0; 42 90 errno = EINVAL; … … 56 104 return -1; 57 105 } 58 106 107 void pthread_key_destructor(void) 108 { 109 pthread_key_pair_t pair; 110 pthread_key_pair_t next; 111 112 for (pair = pair_start; pair != NULL; pair = pair->next) { 113 void *value = pthread_getspecific(pair->key); 114 115 if (value != NULL && pair->destructor != NULL) 116 pair->destructor(value); 117 } 118 } -
pthread_private.h
86 86 pthread_t owner; 87 87 }; 88 88 89 void pthread_key_destructor(void); 90 89 91 #ifdef __cplusplus 90 92 } // extern "C" 91 93 #endif