MOHAMED
MOHAMED

Reputation: 43518

Equivalent of pthread_mutex_lock and pthread_cond_wait in the linux kernel

What is the equivalent of pthread_mutex_lock and pthread_cond_wait in the linux kernel. and how to use them. Could you please provide simple (hello world) examples.

Upvotes: 3

Views: 2518

Answers (3)

MOHAMED
MOHAMED

Reputation: 43518

  • For the mutex (as Als said ):

mutex_lock() and mutex_unlock() and we should init the mutex before we use it with mutex_init() (from #include <linux/mutex.h>)

  • For the equivalent of pthread_cond_wait

wait_event_interruptible() and wake_up_interruptible() and we should init the wait_queue_head with init_waitqueue_head() (from #include <linux/wait.h>)

Upvotes: 4

Kaz
Kaz

Reputation: 58500

I made a mutexes and conditions library for Linux kernel programming long ago (1999-ish) and used in on various projects since.

I called it LMC (linux mutexes and condition variables). This was before there was a mutex type in the kernel.

http://www.kylheku.com/~kaz/lmc.html

Recently, I added a cool new function whose semantics is "give up mutex to wait on a condition variable, and at the same time poll multiple file descriptors, up to a given timeout."

I used this inside kernel thread that monitored various shared objects for updates, and which communicated with kernel sockets at the same time.

Check it out:

/**
 * Atomically give up the mutex and wait on the condition variable.
 * Wake up if the specified timeout elapses, or if a signal is delivered.
 * Additionally, also wait on the specified file descriptors to become
 * ready, combining condition waiting with poll().
 * KCOND_WAIT_SUCCESS means the condition was signaled, or one or more
 * file descriptors are ready.
 * Also, a negative value can be returned indicating an error!
 * (The poll needs to dynamically allocate some memory for the wait table).
 * The timeout is relative to the current time, specifying how long to sleep in
 * jiffies (CPU clock ticks).
 */
int kcond_timed_wait_rel_poll(kcond_t *, kmutex_t *, long, 
                              kcond_poll_t *, unsigned int);

The array of kcond_poll_t structures is something you have to create and fill in your self, and the structure looks like this. There is a type field so you can wait for either sockets (struct socket *) or files (struct file *):

/**
 * Structure for file-descriptor polling condition waits.
 * This resembles struct pollfd, but uses a direct file descriptor
 * pointer rather than a file descriptor number.  Also,
 * it contains the wait queue by which the process is enqueued
 * to wait on that descriptor. Thus our poll function doesn't
 * have to dynamically allocate wait queue tables. It gets
 * them from this array! (But this means that the array cannot
 * be used by multiple threads at the same time to do polling!)
 */
typedef struct {
        kcond_poll_type_t type;  /* Must set this. */
        union {                 /* Must set union field according to type. */
                struct file *file;      
                struct socket *sock;
        } obj;
        short events;           /* And this. */
        short revents;          /* Check response in this. */
        wait_queue_t wait;          /* Internal, don't set. */
        wait_queue_head_t *queue;   /* Internal, don't set */
} kcond_poll_t;

Upvotes: 2

Alok Save
Alok Save

Reputation: 206498

You cannot use any library calls in the kernel space because the kernel modules directly link to kernel.

You can use:

mutex_lock() & mutex_unlock()

They are provided through: linux/mutex.h

Upvotes: 3

Related Questions