Revision e118c57b

View differences:

include/clock.h
11 11
//==========================================================================
12 12

  
13 13
/*--------------------------------------------------------------*
14
  Copyright (C) 2011 Simon Tenbusch
14
 Copyright (C) 2011 Simon Tenbusch
15 15

  
16
  This file is distributed WITHOUT ANY WARRANTY. See the file
17
  `license' for details on this and other legal matters.
18
*--------------------------------------------------------------*/
16
 This file is distributed WITHOUT ANY WARRANTY. See the file
17
 `license' for details on this and other legal matters.
18
 *--------------------------------------------------------------*/
19 19

  
20 20
#ifndef CLOCK_H_
21 21
#define CLOCK_H_
22 22

  
23 23
#include "simkerneldefs.h"
24 24

  
25
class cLock {
25
class cLock
26
{
26 27
public:
27
	    /*
28
	     * Locks the lock
29
	     */
30
	    virtual void lock() { }
31

  
32
	    /*
33
	     * Unlocks the lock
34
	     */
35
	    virtual void unlock() { }
28
    /*
29
     * Locks the lock
30
     */
31
    virtual void lock() = 0;
32
    /*
33
     * Unlocks the lock
34
     */
35
    virtual void unlock() = 0;
36 36

  
37 37
};
38 38

  
include/cnolock.h
11 11
//==========================================================================
12 12

  
13 13
/*--------------------------------------------------------------*
14
  Copyright (C) 2011 Simon Tenbusch
14
 Copyright (C) 2011 Simon Tenbusch
15 15

  
16
  This file is distributed WITHOUT ANY WARRANTY. See the file
17
  `license' for details on this and other legal matters.
16
 This file is distributed WITHOUT ANY WARRANTY. See the file
17
 `license' for details on this and other legal matters.
18 18
 *--------------------------------------------------------------*/
19 19

  
20 20
#ifndef CNOLOCK_H_
......
22 22

  
23 23
#include "clock.h"
24 24

  
25
class cNoLock : public cLock{
25
class cNoLock: public cLock
26
{
26 27
private:
27 28

  
28 29
public:
29
	/**
30
	 * Constructor.
31
	 */
32
	cNoLock();
33

  
34
	/**
35
	 * Destructor.
36
	 */
37
	~cNoLock();
38

  
39
	/*
40
	 * Locks the lock
41
	 */
42
	void lock() { }
43

  
44
	/*
45
	 * Unlocks the lock
46
	 */
47
	void unlock() { }
30
    /**
31
     * Constructor.
32
     */
33
    cNoLock();
34

  
35
    /**
36
     * Destructor.
37
     */
38
    virtual ~cNoLock();
39

  
40
    /*
41
     * Locks the lock
42
     */
43
    void lock()
44
    {
45
    }
46

  
47
    /*
48
     * Unlocks the lock
49
     */
50
    void unlock()
51
    {
52
    }
48 53
};
49 54

  
50 55
#endif /* CNOLOCK_H_ */
include/cpthreadlock.h
11 11
//==========================================================================
12 12

  
13 13
/*--------------------------------------------------------------*
14
  Copyright (C) 2011 Simon Tenbusch
14
 Copyright (C) 2011 Simon Tenbusch
15 15

  
16
  This file is distributed WITHOUT ANY WARRANTY. See the file
17
  `license' for details on this and other legal matters.
16
 This file is distributed WITHOUT ANY WARRANTY. See the file
17
 `license' for details on this and other legal matters.
18 18
 *--------------------------------------------------------------*/
19 19

  
20 20
#ifndef CPTHREADLOCK_H_
......
24 24

  
25 25
#include <pthread.h>
26 26

  
27

  
28
class cPThreadLock : public cLock{
27
class cPThreadLock: public cLock
28
{
29 29
private:
30 30
    pthread_mutex_t mutex;
31 31
public:
32
	/**
33
	 * Constructor.
34
	 */
35
	cPThreadLock();
36

  
37
	/**
38
	 * Destructor.
39
	 */
40
	~cPThreadLock();
41

  
42
	/*
43
	 * Locks the lock
44
	 */
45
	virtual void lock();
46

  
47
	/*
48
	 * Unlocks the lock
49
	 */
50
	virtual void unlock();
32
    /**
33
     * Constructor.
34
     */
35
    cPThreadLock();
36

  
37
    /**
38
     * Destructor.
39
     */
40
    virtual ~cPThreadLock();
41

  
42
    /*
43
     * Locks the lock
44
     */
45
    virtual void lock();
46

  
47
    /*
48
     * Unlocks the lock
49
     */
50
    virtual void unlock();
51 51

  
52 52
};
53 53

  
include/cttaslock.h
11 11
//==========================================================================
12 12

  
13 13
/*--------------------------------------------------------------*
14
  Copyright (C) 2011 Simon Tenbusch
14
 Copyright (C) 2011 Simon Tenbusch
15 15

  
16
  This file is distributed WITHOUT ANY WARRANTY. See the file
17
  `license' for details on this and other legal matters.
16
 This file is distributed WITHOUT ANY WARRANTY. See the file
17
 `license' for details on this and other legal matters.
18 18
 *--------------------------------------------------------------*/
19 19

  
20 20
#ifndef CTTASLOCK_H_
......
23 23
#include "clock.h"
24 24

  
25 25
#ifdef ATOMIC_OPS_DEBUG
26
	#include "catomicopsdebug.h"
26
#include "catomicopsdebug.h"
27 27
#else
28
	#include <atomic_ops.h>
28
#include <atomic_ops.h>
29 29
#endif
30 30

  
31
class cTTASLock : public cLock{
31
class cTTASLock: public cLock
32
{
32 33
private:
33
	AO_t lck;
34
    AO_t lck;
34 35

  
35 36
public:
36
	/**
37
	 * Constructor.
38
	 */
39
	cTTASLock();
40

  
41
	/**
42
	 * Destructor.
43
	 */
44
	~cTTASLock();
45

  
46
	/*
47
	 * Locks the lock
48
	 */
49
	void lock();
50

  
51
	/*
52
	 * Unlocks the lock
53
	 */
54
	void unlock();
37
    /**
38
     * Constructor.
39
     */
40
    cTTASLock();
41

  
42
    /**
43
     * Destructor.
44
     */
45
    virtual ~cTTASLock();
46

  
47
    /*
48
     * Locks the lock
49
     */
50
    void lock();
51

  
52
    /*
53
     * Unlocks the lock
54
     */
55
    void unlock();
55 56

  
56 57
};
57 58

  
src/sim/cpthreadlock.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 "cpthreadlock.h"
18 18

  
19

  
20
	cPThreadLock::cPThreadLock() {
21
		pthread_mutex_init(&mutex, NULL);
22
	}
23

  
24
	cPThreadLock::~cPThreadLock()
25
	{
26
	    pthread_mutex_destroy(&mutex);
27
	}
28

  
29
	void cPThreadLock::lock() {
30
	    pthread_mutex_lock(&mutex);
31
	}
32

  
33
	void cPThreadLock::unlock() {
34
	    pthread_mutex_unlock(&mutex);
35
	}
19
cPThreadLock::cPThreadLock()
20
{
21
    pthread_mutex_init(&mutex, NULL);
22
}
23

  
24
cPThreadLock::~cPThreadLock()
25
{
26
    pthread_mutex_destroy(&mutex);
27
}
28

  
29
void cPThreadLock::lock()
30
{
31
    pthread_mutex_lock(&mutex);
32
}
33

  
34
void cPThreadLock::unlock()
35
{
36
    pthread_mutex_unlock(&mutex);
37
}
36 38

  
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