Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * Copyright (c) 2008 Intel Corporation
 * Author: Matthew Wilcox <willy@linux.intel.com>
 *
 * Please see kernel/locking/semaphore.c for documentation of these functions
 */
#ifndef __LINUX_SEMAPHORE_H
#define __LINUX_SEMAPHORE_H

#include <linux/list.h>
#include <linux/spinlock.h>

/* Please don't access any members of this structure directly */
struct semaphore {
	raw_spinlock_t		lock;
	unsigned int		count;
	struct list_head	wait_list;
};

#define __SEMAPHORE_INITIALIZER(name, n)				\
{									\
	.lock		= __RAW_SPIN_LOCK_UNLOCKED((name).lock),	\
	.count		= n,						\
	.wait_list	= LIST_HEAD_INIT((name).wait_list),		\
}

#define DEFINE_SEMAPHORE(name)	\
	struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)

static inline void sema_init(struct semaphore *sem, int val)
{
	static struct lock_class_key __key;
	*sem = (struct semaphore) __SEMAPHORE_INITIALIZER(*sem, val);
	lockdep_init_map(&sem->lock.dep_map, "semaphore->lock", &__key, 0);
}

#ifdef CONFIG_LOCKDOC

#define down(sem)			\
({			\
	_down(sem);			\
	log_lock(P_WRITE,MK_STRING(sem),sem,__FILE__,__LINE__,__func__,LOCK_NONE);			\
})

#define down_interruptible(sem)			\
({			\
	int ret = _down_interruptible(sem);			\
	if (!ret) {			\
		log_lock(P_WRITE,MK_STRING(sem),sem,__FILE__,__LINE__,__func__,LOCK_NONE);			\
	}			\
	ret;			\
})

#define down_killable(sem)			\
({			\
	int ret = _down_killable(sem);			\
	if (!ret) {			\
		log_lock(P_WRITE,MK_STRING(sem),sem,__FILE__,__LINE__,__func__,LOCK_NONE);			\
	}			\
	ret;			\
})

#define down_trylock(sem)			\
({			\
	int ret = _down_trylock(sem);			\
	if (!ret) {			\
		log_lock(P_WRITE,MK_STRING(sem),sem,__FILE__,__LINE__,__func__,LOCK_NONE);			\
	}			\
	ret;			\
})

#define down_timeout(sem, jiffies)			\
({			\
	int ret = _down_timeout(sem, jiffies);			\
	if (!ret) {			\
		log_lock(P_WRITE,MK_STRING(sem),sem,__FILE__,__LINE__,__func__,LOCK_NONE);			\
	}			\
	ret;			\
})

#define up(sem)			\
({			\
	log_lock(V_WRITE,MK_STRING(sem),sem,__FILE__,__LINE__,__func__,LOCK_NONE);			\
	_up(sem);			\
})

extern void _down(struct semaphore *sem);
extern int __must_check _down_interruptible(struct semaphore *sem);
extern int __must_check _down_killable(struct semaphore *sem);
extern int __must_check _down_trylock(struct semaphore *sem);
extern int __must_check _down_timeout(struct semaphore *sem, long jiffies);
extern void _up(struct semaphore *sem);
#else
extern void down(struct semaphore *sem);
extern int __must_check down_interruptible(struct semaphore *sem);
extern int __must_check down_killable(struct semaphore *sem);
extern int __must_check down_trylock(struct semaphore *sem);
extern int __must_check down_timeout(struct semaphore *sem, long jiffies);
extern void up(struct semaphore *sem);
#endif /* !CONFIG_ESS_LOG_ANALYSIS */

#endif /* __LINUX_SEMAPHORE_H */