Annotation of embedaddon/strongswan/src/libstrongswan/threading/windows/spinlock.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (C) 2013 Martin Willi
3: * Copyright (C) 2013 revosec AG
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 <library.h>
17: #include <threading/spinlock.h>
18:
19: typedef struct private_spinlock_t private_spinlock_t;
20:
21: /**
22: * private data of spinlock
23: */
24: struct private_spinlock_t {
25:
26: /**
27: * public functions
28: */
29: spinlock_t public;
30:
31: /**
32: * wrapped critical section
33: */
34: CRITICAL_SECTION cs;
35: };
36:
37: METHOD(spinlock_t, lock, void,
38: private_spinlock_t *this)
39: {
40: EnterCriticalSection(&this->cs);
41: }
42:
43: METHOD(spinlock_t, unlock, void,
44: private_spinlock_t *this)
45: {
46: LeaveCriticalSection(&this->cs);
47: }
48:
49: METHOD(spinlock_t, destroy, void,
50: private_spinlock_t *this)
51: {
52: DeleteCriticalSection(&this->cs);
53: free(this);
54: }
55:
56: /*
57: * see header file
58: */
59: spinlock_t *spinlock_create()
60: {
61: private_spinlock_t *this;
62:
63: INIT(this,
64: .public = {
65: .lock = _lock,
66: .unlock = _unlock,
67: .destroy = _destroy,
68: },
69: );
70:
71: /* Usually the wait time in a spinlock should be short, so we could have
72: * a high spincount. But having a large/INFINITE spincount does not scale
73: * that well where a spinlock is not the perfect choice for a lock. We
74: * choose the spincount quite arbitrary, so we go to wait if it is not
75: * much more expensive than spinning. */
76: InitializeCriticalSectionAndSpinCount(&this->cs, 256);
77:
78: return &this->public;
79: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>