atomicio.hh revision 11235:4162427127e9
112863Sgabeblack@google.com/* 212863Sgabeblack@google.com * Copyright (c) 2008 The Hewlett-Packard Development Company 312863Sgabeblack@google.com * All rights reserved. 412863Sgabeblack@google.com * 512863Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 612863Sgabeblack@google.com * modification, are permitted provided that the following conditions are 712863Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 812863Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 912863Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 1012863Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1112863Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1212863Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1312863Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1412863Sgabeblack@google.com * this software without specific prior written permission. 1512863Sgabeblack@google.com * 1612863Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1712863Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1812863Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1912863Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2012863Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2112863Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2212863Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2312863Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2412863Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2512863Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2612863Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2712863Sgabeblack@google.com * 2812863Sgabeblack@google.com * Authors: Nathan Binkert 2912863Sgabeblack@google.com */ 3012863Sgabeblack@google.com 3112863Sgabeblack@google.com#ifndef __BASE_ATOMICIO_HH__ 3212950Sgabeblack@google.com#define __BASE_ATOMICIO_HH__ 3312863Sgabeblack@google.com 3412863Sgabeblack@google.com#include <unistd.h> 3512863Sgabeblack@google.com 3612863Sgabeblack@google.com// These functions keep reading/writing, if possible, until all data 3712950Sgabeblack@google.com// has been transferred. Basically, try again when there's no error, 3812863Sgabeblack@google.com// but there is data left also retry on EINTR. 3912863Sgabeblack@google.com// This function blocks until it is done. 4012863Sgabeblack@google.com 4112863Sgabeblack@google.comssize_t atomic_read(int fd, void *s, size_t n); 4212863Sgabeblack@google.comssize_t atomic_write(int fd, const void *s, size_t n); 4312863Sgabeblack@google.com 4412950Sgabeblack@google.com/** 4512863Sgabeblack@google.com * Statically allocate a string and write it to a file descriptor. 4612863Sgabeblack@google.com * 4712863Sgabeblack@google.com * @warning The return value will from atomic_write will be ignored 4812950Sgabeblack@google.com * which means that errors will be ignored. This is normally fine as 4912950Sgabeblack@google.com * this macro is intended to be used in fatal signal handlers where 5012950Sgabeblack@google.com * error handling might not be feasible. 5112950Sgabeblack@google.com */ 5212950Sgabeblack@google.com#define STATIC_MSG(fd, m) \ 5312950Sgabeblack@google.com do { \ 5412863Sgabeblack@google.com static const char msg[] = m; \ 5512950Sgabeblack@google.com atomic_write(fd, msg, sizeof(msg) - 1); \ 5612863Sgabeblack@google.com } while(0) 5712950Sgabeblack@google.com 5812950Sgabeblack@google.com/** 5912863Sgabeblack@google.com * Statically allocate a string and write it to STDERR. 6012950Sgabeblack@google.com * 6112863Sgabeblack@google.com * @warning The return value will from atomic_write will be ignored 6212863Sgabeblack@google.com * which means that errors will be ignored. This is normally fine as 6312863Sgabeblack@google.com * this macro is intended to be used in fatal signal handlers where 6412863Sgabeblack@google.com * error handling might not be feasible. 6512863Sgabeblack@google.com */ 6612950Sgabeblack@google.com#define STATIC_ERR(m) STATIC_MSG(STDERR_FILENO, m) 6712863Sgabeblack@google.com 6812863Sgabeblack@google.com#endif // __BASE_ATOMICIO_HH__ 6912950Sgabeblack@google.com