Ticket #182: key_destructor.diff

File key_destructor.diff, 2.7 KB (added by KO Myung-Hun, 6 years ago)

Patch for key destructor implementation

  • my_os2thread.c

     
    190190                pthread_setspecific(THR_self, NULL);
    191191        }
    192192
     193        // call available destructor associated with key
     194        pthread_key_destructor();
     195
    193196        // thread is joinable, pthread_join is supposed to be called from main thread
    194197
    195198
  • my_os2key.c

     
    2323#define INCL_EXAPIS_MAPPINGS
    2424#include <os2emx.h>
    2525
     26#include <stdlib.h>
     27
    2628#include "pthread.h"
    2729#include "tls.h"
    2830
     31struct pthread_key_pair_t_
     32{
     33    pthread_key_t key;
     34    void (*destructor)(void*);
     35    struct pthread_key_pair_t_ *next;
     36};
     37
     38typedef struct pthread_key_pair_t_ * pthread_key_pair_t;
     39
     40static pthread_key_pair_t pair_start = NULL;
     41
    2942int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
    3043{
     44    pthread_key_pair_t new_pair;
    3145
     46    new_pair = calloc(1, sizeof(*new_pair));
     47    if (new_pair == NULL) {
     48        errno = ENOMEM;
     49        return -1;
     50    }
     51
    3252    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
    3360        return 0;
     61    }
     62
     63    free(new_pair);
     64
    3465    errno = EAGAIN;
    3566    return -1;
    3667}
    3768
    3869int pthread_key_delete(pthread_key_t key)
    3970{
     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
    4088    if (TlsFree((ULONG)key))
    4189        return 0;
    4290    errno = EINVAL;
     
    56104    return -1;
    57105}
    58106
     107void 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

     
    8686  pthread_t owner;
    8787};
    8888
     89void pthread_key_destructor(void);
     90
    8991#ifdef __cplusplus
    9092} // extern "C"
    9193#endif