]>
Commit | Line | Data |
---|---|---|
0fa9c958 MW |
1 | /* |
2 | * Copyright (C) 2013 Martin Willi | |
19ef2aec TB |
3 | * |
4 | * Copyright (C) secunet Security Networks AG | |
0fa9c958 MW |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2 of the License, or (at your | |
9 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | * for more details. | |
15 | */ | |
16 | ||
17 | #include <library.h> | |
18 | #include <threading/spinlock.h> | |
19 | ||
20 | typedef struct private_spinlock_t private_spinlock_t; | |
21 | ||
22 | /** | |
23 | * private data of spinlock | |
24 | */ | |
25 | struct private_spinlock_t { | |
26 | ||
27 | /** | |
28 | * public functions | |
29 | */ | |
30 | spinlock_t public; | |
31 | ||
32 | /** | |
33 | * wrapped critical section | |
34 | */ | |
35 | CRITICAL_SECTION cs; | |
36 | }; | |
37 | ||
38 | METHOD(spinlock_t, lock, void, | |
39 | private_spinlock_t *this) | |
40 | { | |
41 | EnterCriticalSection(&this->cs); | |
42 | } | |
43 | ||
44 | METHOD(spinlock_t, unlock, void, | |
45 | private_spinlock_t *this) | |
46 | { | |
47 | LeaveCriticalSection(&this->cs); | |
48 | } | |
49 | ||
50 | METHOD(spinlock_t, destroy, void, | |
51 | private_spinlock_t *this) | |
52 | { | |
53 | DeleteCriticalSection(&this->cs); | |
54 | free(this); | |
55 | } | |
56 | ||
57 | /* | |
58 | * see header file | |
59 | */ | |
60 | spinlock_t *spinlock_create() | |
61 | { | |
62 | private_spinlock_t *this; | |
63 | ||
64 | INIT(this, | |
65 | .public = { | |
66 | .lock = _lock, | |
67 | .unlock = _unlock, | |
68 | .destroy = _destroy, | |
69 | }, | |
70 | ); | |
71 | ||
72 | /* Usually the wait time in a spinlock should be short, so we could have | |
73 | * a high spincount. But having a large/INFINITE spincount does not scale | |
74 | * that well where a spinlock is not the perfect choice for a lock. We | |
75 | * choose the spincount quite arbitrary, so we go to wait if it is not | |
76 | * much more expensive than spinning. */ | |
77 | InitializeCriticalSectionAndSpinCount(&this->cs, 256); | |
78 | ||
79 | return &this->public; | |
80 | } |