Revision e118c57b src/sim/cttaslock.cc

View differences:

src/sim/cttaslock.cc
7 7
//==========================================================================
8 8

  
9 9
/*--------------------------------------------------------------*
10
  Copyright (C) 2011 Simon Tenbusch
10
 Copyright (C) 2011 Simon Tenbusch
11 11

  
12
  This file is distributed WITHOUT ANY WARRANTY. See the file
13
  `license' for details on this and other legal matters.
12
 This file is distributed WITHOUT ANY WARRANTY. See the file
13
 `license' for details on this and other legal matters.
14 14
 *--------------------------------------------------------------*/
15 15

  
16 16
#include "simkerneldefs.h"
17 17
#include "cttaslock.h"
18 18

  
19

  
20
cTTASLock::cTTASLock() {
21
	AO_store_write(&lck, 0);
19
cTTASLock::cTTASLock()
20
{
21
    AO_store_write(&lck, 0);
22 22
}
23 23

  
24
cTTASLock::~cTTASLock() {
24
cTTASLock::~cTTASLock()
25
{
25 26

  
26 27
}
27 28

  
28 29
static const AO_t unlocked = 0;
29
static const AO_t locked   = 1;
30
static const AO_t locked = 1;
30 31

  
31 32
void cTTASLock::lock()
32 33
{
33
	//
34
	// very simply TTAS based spinlock. Does not scale with the number of CPUs
35
	// and should thus be replaced by more complex locks.
36
	//
37
	while (true)
38
	{
39
		while (AO_load_full(&lck) == locked)
40
		{
41
			__asm__ ("pause");
42
		}
43
		if (AO_compare_and_swap_full(&lck, unlocked, locked) != 0)
44
		{
45
			return;
46
		}
47
	}
34
    //
35
    // very simply TTAS based spinlock. Does not scale with the number of CPUs
36
    // and should thus be replaced by more complex locks.
37
    //
38
    while (true)
39
    {
40
        while (AO_load_full(&lck) == locked)
41
        {
42
            __asm__ ("pause");
43
        }
44
        if (AO_compare_and_swap_full(&lck, unlocked, locked) != 0)
45
        {
46
            return;
47
        }
48
    }
48 49
}
49 50

  
50

  
51 51
void cTTASLock::unlock()
52 52
{
53
	AO_store_full(&lck, unlocked);
53
    AO_store_full(&lck, unlocked);
54 54
}
55 55

  
56

  

Also available in: Unified diff