Commit 8abe24bf authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Add blc_ prefix to make names more consistant

parent 025705cb
......@@ -22,11 +22,13 @@
/**
@defgroup blc_realtime blc_realtime
Few functions and shortcut helping for pseudo (standard unix is not realtime anyway) realtime applications
@{*/
@{*/
///Maximum size of a semaphore name
#define BLC_SEM_NAME_LEN 31 // At least on OSX
/**Write the appropriate message error and exit the program for functions error returned by pthread_... functions. It is similar to EXIT_ON_SYSTEM_ERROR but for pthread_... functions*/
#define EXIT_ON_PTHREAD_ERROR(pthread_error_id, ...) fatal_pthread_error(__FILE__, __FUNCTION__, __LINE__, pthread_error_id, NULL, __VA_ARGS__)
#define BLC_EXIT_ON_PTHREAD_ERROR(blc_pthread_errno, ...) blc_fatal_pthread_error(__FILE__, __FUNCTION__, __LINE__, blc_pthread_errno, NULL, __VA_ARGS__)
/**Execute the command which should be something like 'pthread_...', check the return value and display the error message and exit on error. Error_variable is any int variable you need to provide to the macro. It will be set to 0 in case of success.
It is a good habit to always check the return value of this function. Exemple:
@code{.c}
......@@ -35,15 +37,19 @@
PTHREAD_CHECK(pthread_create(&thread, NULL, your_callback, your_callback_argument), error_id, "Failling creating thread ...");
@endcode
In case of error, you will know the line, the file, the command that was executed and the error in human readable form.*/
#define PTHREAD_CHECK(command, error_variable, ...) do{if (((error_variable)=(command))) fatal_pthread_error(__FILE__, __FUNCTION__, __LINE__, error_variable, STRINGIFY(command), __VA_ARGS__);}while(0)
#define BLC_PTHREAD_CHECK(command, ...) do{if (((blc_pthread_errno)=(command))) blc_fatal_pthread_error(__FILE__, __FUNCTION__, __LINE__, blc_pthread_errno, STRINGIFY(command), __VA_ARGS__);}while(0)
START_EXTERN_C
///Like errno for system error id, it store error id for pthread.
extern int blc_pthread_errno;
///This is called by EXIT_ON_PTHREAD_ERROR and PTHREAD_CHECK, you should not need it directly.
void fatal_pthread_error(const char *name_of_file, const char* name_of_function, int numero_of_line, int error_id, const char *command, const char *message, ...);
void blc_fatal_pthread_error(const char *name_of_file, const char* name_of_function, int numero_of_line, int error_id, const char *command, const char *message, ...);
///Return the time difference between the timeval struct and the actual time and set the time to the current time.
long us_time_diff(struct timeval *time);
long blc_us_time_diff(struct timeval *time);
///Check weather a semaphore is locked. You may use sem_getvalue instead but it does not exist on darwin (Mac OSX).
int sem_is_locked(sem_t *sem);
int blc_sem_is_locked(sem_t *sem);
/**Try to lock the mutex in timeout micro seconds.
@return 1 in case of immediate success, -1 in case of succes after microseconds, 0 in case of failure */
int blc_mutex_trylock_in_time(pthread_mutex_t *mutex, uint32_t microseconds);
......
......@@ -26,7 +26,9 @@
#include <string.h>
void fatal_pthread_error(const char *name_of_file, const char* name_of_function, int numero_of_line, int error_id, const char *command, const char *message, ...)
int blc_pthread_errno=0;
void blc_fatal_pthread_error(const char *name_of_file, const char* name_of_function, int numero_of_line, int error_id, const char *command, const char *message, ...)
{
char const *pthread_message;
......@@ -57,20 +59,18 @@ void fatal_pthread_error(const char *name_of_file, const char* name_of_function,
exit(EXIT_FAILURE);
}
long us_time_diff(struct timeval *previous_time)
long blc_us_time_diff(struct timeval *previous_time)
{
struct timeval new_time;
long delta_useconds;
gettimeofday(&new_time, NULL);
/*if (previous_time->tv_sec == 0) delta_useconds = 0;
else This would be useful to have the fist time null*/
delta_useconds = (new_time.tv_sec - previous_time->tv_sec) * 1000000 + new_time.tv_usec - previous_time->tv_usec;
delta_useconds = (new_time.tv_sec - previous_time->tv_sec) * 1000000 + new_time.tv_usec - previous_time->tv_usec;
*previous_time = new_time;
return delta_useconds;
}
int sem_is_locked(sem_t *sem)
int blc_sem_is_locked(sem_t *sem)
{
if( sem==NULL) EXIT_ON_ERROR("sem is NULL");
if (sem_trywait(sem)==0) //Success to lock (it was not lock)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment