15548Snate@binkert.org/*
25548Snate@binkert.org * Copyright (c) 2008 The Hewlett-Packard Development Company
35548Snate@binkert.org * All rights reserved.
45548Snate@binkert.org *
55548Snate@binkert.org * Redistribution and use in source and binary forms, with or without
65548Snate@binkert.org * modification, are permitted provided that the following conditions are
75548Snate@binkert.org * met: redistributions of source code must retain the above copyright
85548Snate@binkert.org * notice, this list of conditions and the following disclaimer;
95548Snate@binkert.org * redistributions in binary form must reproduce the above copyright
105548Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
115548Snate@binkert.org * documentation and/or other materials provided with the distribution;
125548Snate@binkert.org * neither the name of the copyright holders nor the names of its
135548Snate@binkert.org * contributors may be used to endorse or promote products derived from
145548Snate@binkert.org * this software without specific prior written permission.
155548Snate@binkert.org *
165548Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175548Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
185548Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
195548Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205548Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215548Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225548Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235548Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245548Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255548Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265548Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275548Snate@binkert.org *
285548Snate@binkert.org * Authors: Nathan Binkert
295548Snate@binkert.org */
305548Snate@binkert.org
315548Snate@binkert.org#ifndef __BASE_ATOMICIO_HH__
325548Snate@binkert.org#define __BASE_ATOMICIO_HH__
335548Snate@binkert.org
345548Snate@binkert.org#include <unistd.h>
355548Snate@binkert.org
365548Snate@binkert.org// These functions keep reading/writing, if possible, until all data
375548Snate@binkert.org// has been transferred.  Basically, try again when there's no error,
385548Snate@binkert.org// but there is data left also retry on EINTR.
395548Snate@binkert.org// This function blocks until it is done.
405548Snate@binkert.org
415548Snate@binkert.orgssize_t atomic_read(int fd, void *s, size_t n);
425548Snate@binkert.orgssize_t atomic_write(int fd, const void *s, size_t n);
435548Snate@binkert.org
4411235Sandreas.sandberg@arm.com/**
4511235Sandreas.sandberg@arm.com * Statically allocate a string and write it to a file descriptor.
4611235Sandreas.sandberg@arm.com *
4711235Sandreas.sandberg@arm.com * @warning The return value will from atomic_write will be ignored
4811235Sandreas.sandberg@arm.com * which means that errors will be ignored. This is normally fine as
4911235Sandreas.sandberg@arm.com * this macro is intended to be used in fatal signal handlers where
5011235Sandreas.sandberg@arm.com * error handling might not be feasible.
5111235Sandreas.sandberg@arm.com */
5211235Sandreas.sandberg@arm.com#define STATIC_MSG(fd, m)                                       \
5311235Sandreas.sandberg@arm.com    do {                                                        \
5411235Sandreas.sandberg@arm.com        static const char msg[] = m;                            \
5511235Sandreas.sandberg@arm.com        atomic_write(fd, msg, sizeof(msg) - 1);                 \
5611321Ssteve.reinhardt@amd.com    } while (0)
5711235Sandreas.sandberg@arm.com
5811235Sandreas.sandberg@arm.com/**
5911235Sandreas.sandberg@arm.com * Statically allocate a string and write it to STDERR.
6011235Sandreas.sandberg@arm.com *
6111235Sandreas.sandberg@arm.com * @warning The return value will from atomic_write will be ignored
6211235Sandreas.sandberg@arm.com * which means that errors will be ignored. This is normally fine as
6311235Sandreas.sandberg@arm.com * this macro is intended to be used in fatal signal handlers where
6411235Sandreas.sandberg@arm.com * error handling might not be feasible.
6511235Sandreas.sandberg@arm.com */
6611235Sandreas.sandberg@arm.com#define STATIC_ERR(m) STATIC_MSG(STDERR_FILENO, m)
6711235Sandreas.sandberg@arm.com
685548Snate@binkert.org#endif // __BASE_ATOMICIO_HH__
69