Annotation of embedaddon/strongswan/src/libstrongswan/threading/spinlock.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (C) 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: #include <pthread.h>
17:
18: #include <library.h>
19: #include <utils/debug.h>
20:
21: #include "spinlock.h"
22: #include "mutex.h"
23: #include "lock_profiler.h"
24:
25: typedef struct private_spinlock_t private_spinlock_t;
26:
27: /**
28: * private data
29: */
30: struct private_spinlock_t {
31:
32: /**
33: * public functions
34: */
35: spinlock_t public;
36:
37: #ifdef HAVE_PTHREAD_SPIN_INIT
38:
39: /**
40: * wrapped pthread spin lock
41: */
42: pthread_spinlock_t spinlock;
43:
44: /**
45: * profiling info, if enabled (the mutex below does profile itself)
46: */
47: lock_profile_t profile;
48:
49: #else /* HAVE_PTHREAD_SPIN_INIT */
50:
51: /**
52: * use a mutex if spin locks are not available
53: */
54: mutex_t *mutex;
55:
56: #endif /* HAVE_PTHREAD_SPIN_INIT */
57: };
58:
59: METHOD(spinlock_t, lock, void,
60: private_spinlock_t *this)
61: {
62: #ifdef HAVE_PTHREAD_SPIN_INIT
63: int err;
64:
65: profiler_start(&this->profile);
66: err = pthread_spin_lock(&this->spinlock);
67: if (err)
68: {
69: DBG1(DBG_LIB, "!!! SPIN LOCK LOCK ERROR: %s !!!", strerror(err));
70: }
71: profiler_end(&this->profile);
72: #else
73: this->mutex->lock(this->mutex);
74: #endif
75: }
76:
77: METHOD(spinlock_t, unlock, void,
78: private_spinlock_t *this)
79: {
80: #ifdef HAVE_PTHREAD_SPIN_INIT
81: int err;
82:
83: err = pthread_spin_unlock(&this->spinlock);
84: if (err)
85: {
86: DBG1(DBG_LIB, "!!! SPIN LOCK UNLOCK ERROR: %s !!!", strerror(err));
87: }
88: #else
89: this->mutex->unlock(this->mutex);
90: #endif
91: }
92:
93: METHOD(spinlock_t, destroy, void,
94: private_spinlock_t *this)
95: {
96: #ifdef HAVE_PTHREAD_SPIN_INIT
97: profiler_cleanup(&this->profile);
98: pthread_spin_destroy(&this->spinlock);
99: #else
100: this->mutex->destroy(this->mutex);
101: #endif
102: free(this);
103: }
104:
105: /*
106: * Described in header
107: */
108: spinlock_t *spinlock_create()
109: {
110: private_spinlock_t *this;
111:
112: INIT(this,
113: .public = {
114: .lock = _lock,
115: .unlock = _unlock,
116: .destroy = _destroy,
117: },
118: );
119:
120: #ifdef HAVE_PTHREAD_SPIN_INIT
121: pthread_spin_init(&this->spinlock, PTHREAD_PROCESS_PRIVATE);
122: profiler_init(&this->profile);
123: #else
124: this->mutex = mutex_create(MUTEX_TYPE_DEFAULT);
125: #endif
126:
127: return &this->public;
128: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>