eventfd_write.cpp revision 387d4b7de9af2afd0c37a895ff9d012eb1f66156
1387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes/* 2387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * Copyright (C) 2010 The Android Open Source Project 3387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * All rights reserved. 4387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * 5387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * Redistribution and use in source and binary forms, with or without 6387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * modification, are permitted provided that the following conditions 7387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * are met: 8387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * * Redistributions of source code must retain the above copyright 9387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * notice, this list of conditions and the following disclaimer. 10387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * * Redistributions in binary form must reproduce the above copyright 11387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * notice, this list of conditions and the following disclaimer in 12387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * the documentation and/or other materials provided with the 13387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * distribution. 14387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * 15387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes * SUCH DAMAGE. 27387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes */ 28387d4b7de9af2afd0c37a895ff9d012eb1f66156Elliott Hughes 29defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner#include <sys/eventfd.h> 30defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner#include <unistd.h> 31defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner 32defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner/* We duplicate the GLibc error semantics, which are poorly defined 33defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner * if the read() or write() does not return the proper number of bytes. 34defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner */ 35defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turnerint eventfd_read(int fd, eventfd_t *counter) 36defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner{ 37defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner int ret = read(fd, counter, sizeof(*counter)); 38defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner 39defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner if (ret == sizeof(*counter)) 40defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner return 0; 41defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner 42defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner return -1; 43defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner} 44defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner 45defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turnerint eventfd_write(int fd, eventfd_t counter) 46defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner{ 47defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner int ret = write(fd, &counter, sizeof(counter)); 48defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner 49defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner if (ret == sizeof(counter)) 50defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner return 0; 51defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner 52defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner return -1; 53defd162212de3789d2268a1f3339c2a6097fa825David 'Digit' Turner} 54