summaryrefslogtreecommitdiff
path: root/src/libstrongswan/threading/windows/spinlock.c
blob: 155dd56dc4144baecc20157ef3ec8e7dc439672b (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
/*
 * Copyright (C) 2013 Martin Willi
 * Copyright (C) 2013 revosec AG
 *
 * 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.
 */

#include <library.h>
#include <threading/spinlock.h>

typedef struct private_spinlock_t private_spinlock_t;

/**
 * private data of spinlock
 */
struct private_spinlock_t {

	/**
	 * public functions
	 */
	spinlock_t public;

	/**
	 * wrapped critical section
	 */
	CRITICAL_SECTION cs;
};

METHOD(spinlock_t, lock, void,
	private_spinlock_t *this)
{
	EnterCriticalSection(&this->cs);
}

METHOD(spinlock_t, unlock, void,
	private_spinlock_t *this)
{
	LeaveCriticalSection(&this->cs);
}

METHOD(spinlock_t, destroy, void,
	private_spinlock_t *this)
{
	DeleteCriticalSection(&this->cs);
	free(this);
}

/*
 * see header file
 */
spinlock_t *spinlock_create()
{
	private_spinlock_t *this;

	INIT(this,
		.public = {
			.lock = _lock,
			.unlock = _unlock,
			.destroy = _destroy,
		},
	);

	/* Usually the wait time in a spinlock should be short, so we could have
	 * a high spincount. But having a large/INFINITE spincount does not scale
	 * that well where a spinlock is not the perfect choice for a lock. We
	 * choose the spincount quite arbitrary, so we go to wait if it is not
	 * much more expensive than spinning. */
	InitializeCriticalSectionAndSpinCount(&this->cs, 256);

	return &this->public;
}