Changeset 2346


Ignore:
Timestamp:
Mar 27, 2019, 11:42:32 PM (6 years ago)
Author:
dmik
Message:

pthread: Implement key destructors.

Closes #182.

Location:
pthread/trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified pthread/trunk/src/my_os2key.c

    r1234 r2346  
    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;
     45
     46    new_pair = calloc(1, sizeof(*new_pair));
     47    if (new_pair == NULL) {
     48        errno = ENOMEM;
     49        return -1;
     50    }
    3151
    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;
     
    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;
     
    57105}
    58106
     107void pthread_key_destructor(void)
     108{
     109    pthread_key_pair_t pair;
     110
     111    for (pair = pair_start; pair != NULL; pair = pair->next) {
     112        void *value = pthread_getspecific(pair->key);
     113
     114        if (value != NULL && pair->destructor != NULL) {
     115            pthread_setspecific(pair->key, NULL);
     116            pair->destructor(value);
     117        }
     118    }
     119}
  • TabularUnified pthread/trunk/src/my_os2thread.c

    r2345 r2346  
    189189                pthread_setspecific(THR_self, NULL);
    190190        }
     191
     192        // call all available destructors associated with all keys
     193        pthread_key_destructor();
    191194
    192195        // thread is joinable, pthread_join is supposed to be called from main thread
  • TabularUnified pthread/trunk/src/pthread_private.h

    r2332 r2346  
    8787};
    8888
     89void pthread_key_destructor(void);
     90
    8991#ifdef __cplusplus
    9092} // extern "C"
Note: See TracChangeset for help on using the changeset viewer.