summaryrefslogtreecommitdiff
path: root/src/libstrongswan/threading/thread_value.c
blob: 190b7434f1a7abec903ea8acfe9ef9b52aa192b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
/*
 * Copyright (C) 2009-2012 Tobias Brunner
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#define _GNU_SOURCE
#include <pthread.h>

#include <library.h>

#include "thread_value.h"

typedef struct private_thread_value_t private_thread_value_t;

struct private_thread_value_t {
	/**
	 * Public interface.
	 */
	thread_value_t public;

	/**
	 * Key to access thread-specific values.
	 */
	pthread_key_t key;

	/**
	 * Destructor to cleanup the value of the thread destroying this object
	 */
	thread_cleanup_t destructor;

};

METHOD(thread_value_t, set, void,
	private_thread_value_t *this, void *val)
{
	pthread_setspecific(this->key, val);
}

METHOD(thread_value_t, get, void*,
	private_thread_value_t *this)
{
	return pthread_getspecific(this->key);
}

METHOD(thread_value_t, destroy, void,
	private_thread_value_t *this)
{
	void *val;

	/* the destructor is not called automatically for the thread calling
	 * pthread_key_delete() */
	if (this->destructor)
	{
		val = pthread_getspecific(this->key);
		if (val)
		{
			this->destructor(val);
		}
	}
	pthread_key_delete(this->key);
	free(this);
}

/**
 * Described in header.
 */
thread_value_t *thread_value_create(thread_cleanup_t destructor)
{
	private_thread_value_t *this;

	INIT(this,
		.public = {
			.set = _set,
			.get = _get,
			.destroy = _destroy,
		},
		.destructor = destructor,
	);

	pthread_key_create(&this->key, destructor);
	return &this->public;
}