atomicio.cc revision 11793
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 3111793Sbrandon.potter@amd.com#include "base/atomicio.hh" 3211793Sbrandon.potter@amd.com 335548Snate@binkert.org#include <cerrno> 345548Snate@binkert.org#include <cstdio> 355548Snate@binkert.org 365548Snate@binkert.orgssize_t 375548Snate@binkert.orgatomic_read(int fd, void *s, size_t n) 385548Snate@binkert.org{ 395548Snate@binkert.org char *p = reinterpret_cast<char *>(s); 406227Snate@binkert.org size_t pos = 0; 415548Snate@binkert.org 425548Snate@binkert.org // Keep reading until we've gotten all of the data. 435548Snate@binkert.org while (n > pos) { 445548Snate@binkert.org ssize_t result = read(fd, p + pos, n - pos); 455548Snate@binkert.org 465548Snate@binkert.org // We didn't get any more data, so we should probably punt, 475548Snate@binkert.org // otherwise we'd just keep spinning 485548Snate@binkert.org if (result == 0) 495548Snate@binkert.org break; 505548Snate@binkert.org 515548Snate@binkert.org // If there was an error, try again on EINTR/EAGAIN, pass the 525548Snate@binkert.org // error up otherwise. 535548Snate@binkert.org if (result == -1) { 545548Snate@binkert.org if (errno == EINTR || errno == EAGAIN) 555548Snate@binkert.org continue; 565548Snate@binkert.org return result; 575548Snate@binkert.org } 585548Snate@binkert.org 595548Snate@binkert.org pos += result; 605548Snate@binkert.org } 615548Snate@binkert.org 625548Snate@binkert.org return pos; 635548Snate@binkert.org} 645548Snate@binkert.org 655548Snate@binkert.orgssize_t 665548Snate@binkert.orgatomic_write(int fd, const void *s, size_t n) 675548Snate@binkert.org{ 685548Snate@binkert.org const char *p = reinterpret_cast<const char *>(s); 696227Snate@binkert.org size_t pos = 0; 705548Snate@binkert.org 715548Snate@binkert.org // Keep writing until we've written all of the data 725548Snate@binkert.org while (n > pos) { 735548Snate@binkert.org ssize_t result = write(fd, p + pos, n - pos); 745548Snate@binkert.org 755548Snate@binkert.org // We didn't manage to write anything this time, so we should 765548Snate@binkert.org // probably punt, otherwise we'd just keep spinning 775548Snate@binkert.org if (result == 0) 785548Snate@binkert.org break; 795548Snate@binkert.org 805548Snate@binkert.org // If there was an error, try again on EINTR/EAGAIN, pass the 815548Snate@binkert.org // error up otherwise. 825548Snate@binkert.org if (result == -1) { 835548Snate@binkert.org if (errno == EINTR || errno == EAGAIN) 845548Snate@binkert.org continue; 855548Snate@binkert.org return result; 865548Snate@binkert.org } 875548Snate@binkert.org 885548Snate@binkert.org pos += result; 895548Snate@binkert.org } 905548Snate@binkert.org 915548Snate@binkert.org return pos; 925548Snate@binkert.org} 93