summaryrefslogtreecommitdiff
path: root/libc/src/stdio/fwrite.cpp
blob: 80cf50ca376e421b99449c01b44c05ac26f6489f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//===-- Implementation of fwrite and fwrite_unlocked ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdio/fwrite.h"
#include "src/stdio/FILE.h"
#include "src/threads/mtx_lock.h"
#include "src/threads/mtx_unlock.h"

namespace __llvm_libc {

size_t fwrite_unlocked(const void *__restrict ptr, size_t size, size_t nmeb,
                       __llvm_libc::FILE *__restrict stream) {
  return stream->write(stream, reinterpret_cast<const char *>(ptr),
                       size * nmeb);
}

size_t fwrite(const void *__restrict ptr, size_t size, size_t nmeb,
              __llvm_libc::FILE *__restrict stream) {
  __llvm_libc::mtx_lock(&stream->lock);
  size_t written = fwrite_unlocked(ptr, size, nmeb, stream);
  __llvm_libc::mtx_unlock(&stream->lock);
  return written;
}

} // namespace __llvm_libc