Reindent of server/core/spinlock.c

This commit is contained in:
Johan Wikman
2015-11-30 20:12:58 +02:00
parent 15df33a93f
commit a95be21266
2 changed files with 55 additions and 48 deletions

View File

@ -22,8 +22,8 @@
* @verbatim * @verbatim
* Revision History * Revision History
* *
* Date Who Description * Date Who Description
* 10/06/13 Mark Riddoch Initial implementation * 10/06/13 Mark Riddoch Initial implementation
* *
* @endverbatim * @endverbatim
*/ */
@ -59,9 +59,9 @@ void
spinlock_acquire(SPINLOCK *lock) spinlock_acquire(SPINLOCK *lock)
{ {
#if SPINLOCK_PROFILE #if SPINLOCK_PROFILE
int spins = 0; int spins = 0;
atomic_add(&(lock->waiting), 1); atomic_add(&(lock->waiting), 1);
#endif #endif
#ifdef __GNUC__ #ifdef __GNUC__
@ -70,19 +70,21 @@ int spins = 0;
#else #else
while (atomic_add(&(lock->lock), 1) != 0) while (atomic_add(&(lock->lock), 1) != 0)
{ {
atomic_add(&(lock->lock), -1); atomic_add(&(lock->lock), -1);
#endif #endif
#if SPINLOCK_PROFILE #if SPINLOCK_PROFILE
atomic_add(&(lock->spins), 1); atomic_add(&(lock->spins), 1);
spins++; spins++;
#endif #endif
} }
#if SPINLOCK_PROFILE #if SPINLOCK_PROFILE
if (spins) if (spins)
{ {
lock->contended++; lock->contended++;
if (lock->maxspins < spins) if (lock->maxspins < spins)
{
lock->maxspins = spins; lock->maxspins = spins;
}
} }
lock->acquired++; lock->acquired++;
lock->owner = THREAD_SHELF(); lock->owner = THREAD_SHELF();
@ -125,7 +127,9 @@ spinlock_release(SPINLOCK *lock)
{ {
#if SPINLOCK_PROFILE #if SPINLOCK_PROFILE
if (lock->waiting > lock->max_waiting) if (lock->waiting > lock->max_waiting)
{
lock->max_waiting = lock->waiting; lock->max_waiting = lock->waiting;
}
#endif #endif
#ifdef __GNUC__ #ifdef __GNUC__
__sync_synchronize(); /* Memory barrier. */ __sync_synchronize(); /* Memory barrier. */
@ -144,29 +148,31 @@ spinlock_release(SPINLOCK *lock)
* form the spinlock code and also to facilitate other uses of the * form the spinlock code and also to facilitate other uses of the
* statistics reporting. * statistics reporting.
* *
* @param lock The spinlock to report on * @param lock The spinlock to report on
* @param reporter The callback function to pass the statistics to * @param reporter The callback function to pass the statistics to
* @param hdl A handle that is passed to the reporter function * @param hdl A handle that is passed to the reporter function
*/ */
void void
spinlock_stats(SPINLOCK *lock, void (*reporter)(void *, char *, int), void *hdl) spinlock_stats(SPINLOCK *lock, void (*reporter)(void *, char *, int), void *hdl)
{ {
#if SPINLOCK_PROFILE #if SPINLOCK_PROFILE
reporter(hdl, "Spinlock acquired", lock->acquired); reporter(hdl, "Spinlock acquired", lock->acquired);
if (lock->acquired) if (lock->acquired)
{ {
reporter(hdl, "Total no. of spins", lock->spins); reporter(hdl, "Total no. of spins", lock->spins);
reporter(hdl, "Average no. of spins (overall)", reporter(hdl, "Average no. of spins (overall)",
lock->spins / lock->acquired); lock->spins / lock->acquired);
if (lock->contended) if (lock->contended)
reporter(hdl, "Average no. of spins (when contended)", {
lock->spins / lock->contended); reporter(hdl, "Average no. of spins (when contended)",
reporter(hdl, "Maximum no. of spins", lock->maxspins); lock->spins / lock->contended);
reporter(hdl, "Maximim no. of blocked threads", }
lock->max_waiting); reporter(hdl, "Maximum no. of spins", lock->maxspins);
reporter(hdl, "Contended locks", lock->contended); reporter(hdl, "Maximim no. of blocked threads",
reporter(hdl, "Contention percentage", lock->max_waiting);
(lock->contended * 100) / lock->acquired); reporter(hdl, "Contended locks", lock->contended);
} reporter(hdl, "Contention percentage",
(lock->contended * 100) / lock->acquired);
}
#endif #endif
} }

View File

@ -31,7 +31,7 @@
#include <thread.h> #include <thread.h>
#include <stdbool.h> #include <stdbool.h>
#define SPINLOCK_PROFILE 0 #define SPINLOCK_PROFILE 0
/** /**
* The spinlock structure. * The spinlock structure.
@ -43,24 +43,25 @@
* a number of profile related fields that count the number of spins, number * a number of profile related fields that count the number of spins, number
* of waiting threads and the number of times the lock has been acquired. * of waiting threads and the number of times the lock has been acquired.
*/ */
typedef struct spinlock { typedef struct spinlock
int lock; /*< Is the lock held? */ {
int lock; /*< Is the lock held? */
#if SPINLOCK_PROFILE #if SPINLOCK_PROFILE
int spins; /*< Number of spins on this lock */ int spins; /*< Number of spins on this lock */
int maxspins; /*< Max no of spins to acquire lock */ int maxspins; /*< Max no of spins to acquire lock */
int acquired; /*< No. of times lock was acquired */ int acquired; /*< No. of times lock was acquired */
int waiting; /*< No. of threads acquiring this lock */ int waiting; /*< No. of threads acquiring this lock */
int max_waiting; /*< Max no of threads waiting for lock */ int max_waiting; /*< Max no of threads waiting for lock */
int contended; /*< No. of times acquire was contended */ int contended; /*< No. of times acquire was contended */
THREAD owner; /*< Last owner of this lock */ THREAD owner; /*< Last owner of this lock */
#endif #endif
} SPINLOCK; } SPINLOCK;
#ifndef TRUE #ifndef TRUE
#define TRUE true #define TRUE true
#endif #endif
#ifndef FALSE #ifndef FALSE
#define FALSE false #define FALSE false
#endif #endif
#if SPINLOCK_PROFILE #if SPINLOCK_PROFILE
@ -71,10 +72,10 @@ typedef struct spinlock {
#define SPINLOCK_IS_LOCKED(l) ((l)->lock != 0 ? true : false) #define SPINLOCK_IS_LOCKED(l) ((l)->lock != 0 ? true : false)
extern void spinlock_init(SPINLOCK *lock); extern void spinlock_init(SPINLOCK *lock);
extern void spinlock_acquire(SPINLOCK *lock); extern void spinlock_acquire(SPINLOCK *lock);
extern int spinlock_acquire_nowait(SPINLOCK *lock); extern int spinlock_acquire_nowait(SPINLOCK *lock);
extern void spinlock_release(SPINLOCK *lock); extern void spinlock_release(SPINLOCK *lock);
extern void spinlock_stats(SPINLOCK *lock, extern void spinlock_stats(SPINLOCK *lock, void (*reporter)(void *, char *, int), void *hdl);
void (*reporter)(void *, char *, int), void *hdl);
#endif #endif