gem5  v20.1.0.0
atomicio.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 The Hewlett-Packard Development Company
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "base/atomicio.hh"
30 
31 #include <cerrno>
32 #include <cstdio>
33 
34 ssize_t
35 atomic_read(int fd, void *s, size_t n)
36 {
37  char *p = reinterpret_cast<char *>(s);
38  size_t pos = 0;
39 
40  // Keep reading until we've gotten all of the data.
41  while (n > pos) {
42  ssize_t result = read(fd, p + pos, n - pos);
43 
44  // We didn't get any more data, so we should probably punt,
45  // otherwise we'd just keep spinning
46  if (result == 0)
47  break;
48 
49  // If there was an error, try again on EINTR/EAGAIN, pass the
50  // error up otherwise.
51  if (result == -1) {
52  if (errno == EINTR || errno == EAGAIN)
53  continue;
54  return result;
55  }
56 
57  pos += result;
58  }
59 
60  return pos;
61 }
62 
63 ssize_t
64 atomic_write(int fd, const void *s, size_t n)
65 {
66  const char *p = reinterpret_cast<const char *>(s);
67  size_t pos = 0;
68 
69  // Keep writing until we've written all of the data
70  while (n > pos) {
71  ssize_t result = write(fd, p + pos, n - pos);
72 
73  // We didn't manage to write anything this time, so we should
74  // probably punt, otherwise we'd just keep spinning
75  if (result == 0)
76  break;
77 
78  // If there was an error, try again on EINTR/EAGAIN, pass the
79  // error up otherwise.
80  if (result == -1) {
81  if (errno == EINTR || errno == EAGAIN)
82  continue;
83  return result;
84  }
85 
86  pos += result;
87  }
88 
89  return pos;
90 }
atomicio.hh
ArmISA::fd
Bitfield< 14, 12 > fd
Definition: types.hh:159
ArmISA::n
Bitfield< 31 > n
Definition: miscregs_types.hh:450
atomic_read
ssize_t atomic_read(int fd, void *s, size_t n)
Definition: atomicio.cc:35
atomic_write
ssize_t atomic_write(int fd, const void *s, size_t n)
Definition: atomicio.cc:64
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
ArmISA::s
Bitfield< 4 > s
Definition: miscregs_types.hh:556

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17