Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 139198
b: refs/heads/master
c: bcd0b23
h: refs/heads/master
v: v3
  • Loading branch information
Davide Libenzi authored and Linus Torvalds committed Apr 1, 2009
1 parent 55f7af7 commit 4b48d81
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: 4f0989dbfa8d18dd17c32120aac1eb3e906a62a2
refs/heads/master: bcd0b235bf3808dec5115c381cd55568f63b85f0
20 changes: 11 additions & 9 deletions trunk/fs/eventfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct eventfd_ctx {
* issue a wakeup.
*/
__u64 count;
unsigned int flags;
};

/*
Expand Down Expand Up @@ -87,22 +88,20 @@ static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count,
{
struct eventfd_ctx *ctx = file->private_data;
ssize_t res;
__u64 ucnt;
__u64 ucnt = 0;
DECLARE_WAITQUEUE(wait, current);

if (count < sizeof(ucnt))
return -EINVAL;
spin_lock_irq(&ctx->wqh.lock);
res = -EAGAIN;
ucnt = ctx->count;
if (ucnt > 0)
if (ctx->count > 0)
res = sizeof(ucnt);
else if (!(file->f_flags & O_NONBLOCK)) {
__add_wait_queue(&ctx->wqh, &wait);
for (res = 0;;) {
set_current_state(TASK_INTERRUPTIBLE);
if (ctx->count > 0) {
ucnt = ctx->count;
res = sizeof(ucnt);
break;
}
Expand All @@ -117,8 +116,9 @@ static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count,
__remove_wait_queue(&ctx->wqh, &wait);
__set_current_state(TASK_RUNNING);
}
if (res > 0) {
ctx->count = 0;
if (likely(res > 0)) {
ucnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count;
ctx->count -= ucnt;
if (waitqueue_active(&ctx->wqh))
wake_up_locked(&ctx->wqh);
}
Expand Down Expand Up @@ -166,7 +166,7 @@ static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t c
__remove_wait_queue(&ctx->wqh, &wait);
__set_current_state(TASK_RUNNING);
}
if (res > 0) {
if (likely(res > 0)) {
ctx->count += ucnt;
if (waitqueue_active(&ctx->wqh))
wake_up_locked(&ctx->wqh);
Expand Down Expand Up @@ -207,7 +207,7 @@ SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC);
BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK);

if (flags & ~(EFD_CLOEXEC | EFD_NONBLOCK))
if (flags & ~EFD_FLAGS_SET)
return -EINVAL;

ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
Expand All @@ -216,13 +216,14 @@ SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)

init_waitqueue_head(&ctx->wqh);
ctx->count = count;
ctx->flags = flags;

/*
* When we call this, the initialization must be complete, since
* anon_inode_getfd() will install the fd.
*/
fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx,
flags & (O_CLOEXEC | O_NONBLOCK));
flags & EFD_SHARED_FCNTL_FLAGS);
if (fd < 0)
kfree(ctx);
return fd;
Expand All @@ -232,3 +233,4 @@ SYSCALL_DEFINE1(eventfd, unsigned int, count)
{
return sys_eventfd2(count, 0);
}

12 changes: 11 additions & 1 deletion trunk/include/linux/eventfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@
/* For O_CLOEXEC and O_NONBLOCK */
#include <linux/fcntl.h>

/* Flags for eventfd2. */
/*
* CAREFUL: Check include/asm-generic/fcntl.h when defining
* new flags, since they might collide with O_* ones. We want
* to re-use O_* flags that couldn't possibly have a meaning
* from eventfd, in order to leave a free define-space for
* shared O_* flags.
*/
#define EFD_SEMAPHORE (1 << 0)
#define EFD_CLOEXEC O_CLOEXEC
#define EFD_NONBLOCK O_NONBLOCK

#define EFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK)
#define EFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS | EFD_SEMAPHORE)

struct file *eventfd_fget(int fd);
int eventfd_signal(struct file *file, int n);

Expand Down

0 comments on commit 4b48d81

Please sign in to comment.