Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
Lock Struct Reference

#include <Lock.h>

Overview

POSIX Threads locks.

Mediate access to a critical section by enforcing mutual exclusion.

Definition at line 42 of file Lock.h.

Inheritance diagram for Lock:
Object Condition

Properties

Object object
 The superclass. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_Lock (void)
 The Lock archetype. More...
 
Lockinit (Lock *self)
 Initializes this Lock. More...
 
void lock (Lock *self)
 Acquire this lock, waiting indefinitely. More...
 
_Bool tryLock (Lock *self)
 Attempt to acquire this lock immediately. More...
 
void unlock (Lock *self)
 Release this Lock. More...
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype. More...
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object. More...
 
void dealloc (Object *self)
 Frees all resources held by this Object. More...
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object. More...
 
_Bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object. More...
 
_Bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership. More...
 

Protected Attributes

LockInterface * interface
 The interface. More...
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface. More...
 

Property Details

◆ interface

LockInterface* Lock::interface
protected

The interface.

Definition at line 53 of file Lock.h.

◆ object

Object Lock::object

The superclass.

Definition at line 47 of file Lock.h.

Method Details

◆ _Lock()

Class * _Lock ( void  )

The Lock archetype.

Returns
The Lock Class.

Definition at line 129 of file Lock.c.

129 {
130 static Class *clazz;
131 static Once once;
132
133 do_once(&once, {
134 clazz = _initialize(&(const ClassDef) {
135 .name = "Lock",
136 .superclass = _Object(),
137 .instanceSize = sizeof(Lock),
138 .interfaceOffset = offsetof(Lock, interface),
139 .interfaceSize = sizeof(LockInterface),
141 });
142 });
143
144 return clazz;
145}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: Lock.c:114
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
The runtime representation of a Class.
Definition: Class.h:95
POSIX Threads locks.
Definition: Lock.h:42
LockInterface * interface
The interface.
Definition: Lock.h:53
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ init()

Lock * init ( Lock self)

Initializes this Lock.

Parameters
selfThe Lock.
Returns
The initialized Lock, or NULL on error.

Definition at line 62 of file Lock.c.

62 {
63
64 self = (Lock *) super(Object, self, init);
65 if (self) {
66
67 self->lock = calloc(1, sizeof(pthread_mutex_t));
68 assert(self->lock);
69
70 const int err = pthread_mutex_init(self->lock, NULL);
71 assert(err == 0);
72 }
73
74 return self;
75}
#define super(type, obj, method,...)
Lock * init(Lock *self)
Initializes this Lock.
Definition: Lock.c:62
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ lock()

void lock ( Lock self)

Acquire this lock, waiting indefinitely.

Parameters
selfThe Lock.

Definition at line 81 of file Lock.c.

81 {
82
83 int err = pthread_mutex_lock(self->lock);
84 assert(err == 0);
85}

◆ tryLock()

_Bool tryLock ( Lock self)

Attempt to acquire this lock immediately.

Parameters
selfThe Lock.
Returns
true if the Lock was acquired, false otherwise.

Definition at line 91 of file Lock.c.

91 {
92
93 int err = pthread_mutex_trylock(self->lock);
94 assert(err == 0 || err == EBUSY);
95
96 return err == 0;
97}

◆ unlock()

void unlock ( Lock self)

Release this Lock.

Parameters
selfThe Lock.

Definition at line 103 of file Lock.c.

103 {
104
105 int err = pthread_mutex_unlock(self->lock);
106 assert(err == 0);
107}

The documentation for this struct was generated from the following files: