Skip to content
Snippets Groups Projects
Select Git revision
  • 6f1981a5a868a9df1058e40b702ecf9b993691d6
  • master default
  • test
  • rahix/hw-lock-new-mutex
  • dx/somewhat-more-dynamic-config
  • schneider/sdk-0.2.1-7
  • schneider/bsec
  • dx/meh-bdf-to-stm
  • dx/flatten-config-module
  • genofire/ble-follow-py
  • schneider/ble-stability
  • schneider/ble-stability-new-phy
  • add_menu_vibration
  • plaetzchen/ios-workaround
  • blinkisync-as-preload
  • schneider/max30001-pycardium
  • schneider/max30001-epicaridum
  • schneider/max30001
  • schneider/stream-locks
  • schneider/fundamental-test
  • schneider/ble-buffers
  • v1.12
  • v1.11
  • v1.10
  • v1.9
  • v1.8
  • v1.7
  • v1.6
  • v1.5
  • v1.4
  • v1.3
  • v1.2
  • v1.1
  • v1.0
  • release-1
  • bootloader-v1
  • v0.0
37 results

code-style.sh

Blame
  • Forked from card10 / firmware
    Source project has a limited visibility.
    mutex.c 1.22 KiB
    #include "modules/mutex.h"
    
    #include <assert.h>
    
    void _mutex_create(struct mutex *m, const char *name)
    {
    	/* Assert that the mutex has not been initialized already */
    	assert(m->name == NULL);
    
    	/*
    	 * The name is just the parameter stringified which is almost always a
    	 * pointer.  If it is, skip over the '&' because it adds no value as
    	 * part of the name.
    	 */
    	if (name[0] == '&') {
    		m->name = &name[1];
    	} else {
    		m->name = name;
    	}
    
    	m->_rtos_mutex = xSemaphoreCreateMutexStatic(&m->_rtos_mutex_data);
    }
    
    void mutex_lock(struct mutex *m)
    {
    	int ret = xSemaphoreTake(m->_rtos_mutex, portMAX_DELAY);
    
    	/* Ensure locking was actually successful */
    	assert(ret == pdTRUE);
    }
    
    bool mutex_trylock(struct mutex *m)
    {
    	int ret = xSemaphoreTake(m->_rtos_mutex, 0);
    	return ret == pdTRUE;
    }
    
    void mutex_unlock(struct mutex *m)
    {
    	/* Ensure that only the owner can unlock a mutex */
    	assert(mutex_get_owner(m) == xTaskGetCurrentTaskHandle());
    
    	int ret = xSemaphoreGive(m->_rtos_mutex);
    
    	/*
    	 * Ensure that unlocking was successful; that is, the mutex must have
    	 * been acquired previously (no multiple unlocks).
    	 */
    	assert(ret == pdTRUE);
    }
    
    TaskHandle_t mutex_get_owner(struct mutex *m)
    {
    	return xSemaphoreGetMutexHolder(m->_rtos_mutex);
    }