File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / strongswan / src / libstrongswan / threading / thread_value.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Jun 3 09:46:44 2020 UTC (4 years, 3 months ago) by misho
Branches: strongswan, MAIN
CVS tags: v5_9_2p0, v5_8_4p7, HEAD
Strongswan

    1: /*
    2:  * Copyright (C) 2009-2012 Tobias Brunner
    3:  * HSR Hochschule fuer Technik Rapperswil
    4:  *
    5:  * This program is free software; you can redistribute it and/or modify it
    6:  * under the terms of the GNU General Public License as published by the
    7:  * Free Software Foundation; either version 2 of the License, or (at your
    8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
    9:  *
   10:  * This program is distributed in the hope that it will be useful, but
   11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   13:  * for more details.
   14:  */
   15: 
   16: #define _GNU_SOURCE
   17: #include <pthread.h>
   18: 
   19: #include <library.h>
   20: 
   21: #include "thread_value.h"
   22: 
   23: typedef struct private_thread_value_t private_thread_value_t;
   24: 
   25: struct private_thread_value_t {
   26: 	/**
   27: 	 * Public interface.
   28: 	 */
   29: 	thread_value_t public;
   30: 
   31: 	/**
   32: 	 * Key to access thread-specific values.
   33: 	 */
   34: 	pthread_key_t key;
   35: 
   36: 	/**
   37: 	 * Destructor to cleanup the value of the thread destroying this object
   38: 	 */
   39: 	thread_cleanup_t destructor;
   40: 
   41: };
   42: 
   43: METHOD(thread_value_t, set, void,
   44: 	private_thread_value_t *this, void *val)
   45: {
   46: 	pthread_setspecific(this->key, val);
   47: }
   48: 
   49: METHOD(thread_value_t, get, void*,
   50: 	private_thread_value_t *this)
   51: {
   52: 	return pthread_getspecific(this->key);
   53: }
   54: 
   55: METHOD(thread_value_t, destroy, void,
   56: 	private_thread_value_t *this)
   57: {
   58: 	void *val;
   59: 
   60: 	/* the destructor is not called automatically for the thread calling
   61: 	 * pthread_key_delete() */
   62: 	if (this->destructor)
   63: 	{
   64: 		val = pthread_getspecific(this->key);
   65: 		if (val)
   66: 		{
   67: 			this->destructor(val);
   68: 		}
   69: 	}
   70: 	pthread_key_delete(this->key);
   71: 	free(this);
   72: }
   73: 
   74: /**
   75:  * Described in header.
   76:  */
   77: thread_value_t *thread_value_create(thread_cleanup_t destructor)
   78: {
   79: 	private_thread_value_t *this;
   80: 
   81: 	INIT(this,
   82: 		.public = {
   83: 			.set = _set,
   84: 			.get = _get,
   85: 			.destroy = _destroy,
   86: 		},
   87: 		.destructor = destructor,
   88: 	);
   89: 
   90: 	pthread_key_create(&this->key, destructor);
   91: 	return &this->public;
   92: }
   93: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>