File: Lock.h

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (62 lines) | stat: -rw-r--r-- 1,251 bytes parent folder | download | duplicates (2)
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
#pragma once
#include "Core/Object.h"
#include "OS/Sync.h"

namespace storm {
	STORM_PKG(core.sync);

	/**
	 * A recursive lock usable from within Storm. If possible, use a plain os::Lock or os::Sema from
	 * C++ as it does not require any separate allocations.
	 *
	 * This object slightly breaks the expected semantics of Storm as it does not make sense to copy
	 * a lock. Instead, all copies will refer to the same semaphore.
	 *
	 * Note: usually it is not required to use locks within Storm as the threading model takes care
	 * of most cases where locks would be needed.
	 */
	class Lock : public Object {
		STORM_CLASS;
	public:
		// Create.
		STORM_CTOR Lock();
		Lock(const Lock &o);

		// Destroy.
		~Lock();

		// Deep copy.
		void STORM_FN deepCopy(CloneEnv *e);

		// Lock guard for C++ and Storm.
		class Guard {
			STORM_VALUE;
		public:
			STORM_CTOR Guard(Lock *o);
			~Guard();
		private:
			Lock *lock;

			// Actually implemented, as they might be called from Storm.
			Guard(const Guard &o);
			Guard &operator =(const Guard &o);
		};

	private:
		struct Data {
			size_t refs;
			size_t owner;
			size_t recursion;
			os::Sema sema;

			Data();
		};

		Data *alloc;

		// Lock and unlock.
		void lock();
		void unlock();
	};

}