µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
os::posix::block_device_lockable< T, L > Class Template Reference

#include <block-device.h>

+ Inheritance diagram for os::posix::block_device_lockable< T, L >:

Public Types

using blknum_t = std::size_t
 
using lockable_type = L
 
using value_type = T
 
Types & Constants
enum class  type : type_t {
  unknown = 0 ,
  not_set = 1 << 0 ,
  char_device = 1 << 1 ,
  block_device = 1 << 2 ,
  tty = 1 << 3 ,
  file = 1 << 4 ,
  socket = 1 << 5
}
 
using type_t = unsigned int
 

Public Member Functions

Constructors & Destructor
template<typename... Args>
 block_device_lockable (const char *name, lockable_type &locker, Args &&... args)
 
virtual ~block_device_lockable () override
 
Public Member Functions
virtual int close (void) override
 
virtual ssize_t read (void *buf, std::size_t nbyte) override
 
virtual ssize_t write (const void *buf, std::size_t nbyte) override
 
virtual ssize_t writev (const iovec *iov, int iovcnt) override
 
virtual int vfcntl (int cmd, std::va_list args) override
 
virtual int vioctl (int request, std::va_list args) override
 
virtual off_t lseek (off_t offset, int whence) override
 
virtual ssize_t read_block (void *buf, blknum_t blknum, std::size_t nblocks=1) override
 
virtual ssize_t write_block (const void *buf, blknum_t blknum, std::size_t nblocks=1) override
 
virtual void sync (void) override
 
value_typeimpl (void) const
 
Public Member Functions
blknum_t blocks (void)
 
std::size_t block_logical_size_bytes (void)
 
std::size_t block_physical_size_bytes (void)
 
Public Member Functions
int fcntl (int cmd,...)
 
int isatty (void)
 
virtual int fstat (struct stat *buf)
 
type_t get_type (void) const
 
file_descriptor_t file_descriptor (void) const
 
bool is_opened (void)
 

Protected Member Functions

Private Member Functions
void file_descriptor (file_descriptor_t fildes)
 
void clear_file_descriptor (void)
 
ioalloc_file_descriptor (void)
 

Public Member Functions

int open (const char *path=nullptr, int oflag=0,...)
 
int vopen (const char *path, int oflag, std::va_list args)
 
int ioctl (int request,...)
 
virtual bool match_name (const char *name) const
 
const char * name (void) const
 
static const char * device_prefix (void)
 

Detailed Description

template<typename T, typename L>
class os::posix::block_device_lockable< T, L >

Definition at line 334 of file block-device.h.

Member Typedef Documentation

◆ blknum_t

using os::posix::block_device::blknum_t = std::size_t
inherited

Definition at line 65 of file block-device.h.

◆ lockable_type

template<typename T , typename L >
using os::posix::block_device_lockable< T, L >::lockable_type = L

Definition at line 340 of file block-device.h.

◆ type_t

using os::posix::io::type_t = unsigned int
inherited

Definition at line 125 of file io.h.

◆ value_type

template<typename T , typename L >
using os::posix::block_device_lockable< T, L >::value_type = T

Definition at line 339 of file block-device.h.

Member Enumeration Documentation

◆ type

enum class os::posix::io::type : type_t
stronginherited
Enumerator
unknown 
not_set 
char_device 
block_device 
tty 
file 
socket 

Definition at line 126 of file io.h.

127 {
128 unknown = 0,
129 not_set = 1 << 0,
130 char_device = 1 << 1,
131 block_device = 1 << 2,
132 tty = 1 << 3,
133 file = 1 << 4,
134 socket = 1 << 5
135 };

Constructor & Destructor Documentation

◆ block_device_lockable()

template<typename T , typename L >
template<typename... Args>
os::posix::block_device_lockable< T, L >::block_device_lockable ( const char *  name,
lockable_type locker,
Args &&...  args 
)

Definition at line 519 of file block-device.h.

522 : block_device{ impl_instance_, name }, //
523 impl_instance_{ std::forward<Args> (args)... }, //
524 locker_ (locker)
525 {
526#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE)
527 trace::printf ("block_device_lockable::%s(\"%s\")=@%p\n", __func__,
528 name_, this);
529#endif
530 }
block_device(block_device_impl &impl, const char *name)
const char * name(void) const
Definition device.h:304
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59

References os::trace::printf().

◆ ~block_device_lockable()

template<typename T , typename L >
os::posix::block_device_lockable< T, L >::~block_device_lockable
overridevirtual

Definition at line 533 of file block-device.h.

534 {
535#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE)
536 trace::printf ("block_device_lockable::%s() @%p %s\n", __func__, this,
537 name_);
538#endif
539 }

References os::trace::printf().

Member Function Documentation

◆ alloc_file_descriptor()

io * os::posix::io::alloc_file_descriptor ( void  )
protectedinherited

Definition at line 189 of file io.cpp.

190 {
191#if defined(OS_TRACE_POSIX_IO_IO)
192 trace::printf ("io::%s() @%p\n", __func__, this);
193#endif
194
196 if (fd < 0)
197 {
198 // If allocation failed, close this object.
199 impl ().do_close ();
201 return nullptr;
202 }
203
204#if defined(OS_TRACE_POSIX_IO_IO)
205 trace::printf ("io::%s() @%p fd=%d\n", __func__, this, fd);
206#endif
207
208 // Return a valid pointer to an object derived from `io`.
209 return this;
210 }
virtual int do_close(void)=0
io_impl & impl(void) const
Definition io.h:464
void clear_file_descriptor(void)
Definition io.h:446

References os::posix::file_descriptors_manager::allocate(), os::posix::io::clear_file_descriptor(), os::posix::io_impl::do_close(), os::posix::io::impl(), and os::trace::printf().

Referenced by os::posix::socket::accept(), os::posix::socket(), os::posix::device::vopen(), and os::posix::file_system::vopen().

◆ block_logical_size_bytes()

std::size_t os::posix::block_device::block_logical_size_bytes ( void  )
inlineinherited
Returns
The number of bytes in a block.

Definition at line 461 of file block-device.h.

462 {
463 return impl ().block_logical_size_bytes_;
464 }
block_device_impl & impl(void) const

References os::posix::block_device::impl().

◆ block_physical_size_bytes()

std::size_t os::posix::block_device::block_physical_size_bytes ( void  )
inlineinherited

Definition at line 467 of file block-device.h.

468 {
469 return impl ().block_physical_size_bytes_;
470 }

References os::posix::block_device::impl().

◆ blocks()

block_device::blknum_t os::posix::block_device::blocks ( void  )
inlineinherited
Returns
The number of blocks.

Definition at line 455 of file block-device.h.

456 {
457 return impl ().num_blocks_;
458 }

References os::posix::block_device::impl().

◆ clear_file_descriptor()

void os::posix::io::clear_file_descriptor ( void  )
inlineprotectedinherited

Definition at line 446 of file io.h.

447 {
448 file_descriptor_ = no_file_descriptor;
449 }
constexpr file_descriptor_t no_file_descriptor
Definition types.h:44

References os::posix::no_file_descriptor.

Referenced by os::posix::io::alloc_file_descriptor().

◆ close()

template<typename T , typename L >
int os::posix::block_device_lockable< T, L >::close ( void  )
overridevirtual

Reimplemented from os::posix::device.

Definition at line 545 of file block-device.h.

546 {
547#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE)
548 trace::printf ("block_device_lockable::%s() @%p\n", __func__, this);
549#endif
550
551 std::lock_guard<L> lock{ locker_ };
552
553 return block_device::close ();
554 }
virtual int close(void) override
Definition device.cpp:111
port::scheduler::state_t lock(void)

References os::posix::device::close(), and os::trace::printf().

◆ device_prefix()

const char * os::posix::device::device_prefix ( void  )
inlinestaticinherited

Definition at line 316 of file device.h.

317 {
319 }
#define OS_STRING_POSIX_DEVICE_PREFIX
Definition device.h:32

References OS_STRING_POSIX_DEVICE_PREFIX.

◆ fcntl()

int os::posix::io::fcntl ( int  cmd,
  ... 
)
inherited

Definition at line 363 of file io.cpp.

364 {
365 // Forward to the variadic version of the function.
366 std::va_list args;
367 va_start (args, cmd);
368 int ret = vfcntl (cmd, args);
369 va_end (args);
370
371 return ret;
372 }
virtual int vfcntl(int cmd, std::va_list args)
Definition io.cpp:380

References os::posix::io::vfcntl().

◆ file_descriptor() [1/2]

void os::posix::io::file_descriptor ( file_descriptor_t  fildes)
inlineprotectedinherited

Definition at line 440 of file io.h.

441 {
442 file_descriptor_ = fildes;
443 }

◆ file_descriptor() [2/2]

file_descriptor_t os::posix::io::file_descriptor ( void  ) const
inlineinherited

Definition at line 452 of file io.h.

453 {
454 return file_descriptor_;
455 }

Referenced by os::posix::file_descriptors_manager::allocate(), os::posix::file_descriptors_manager::assign(), os::posix::device::vopen(), and os::posix::vopen().

◆ fstat()

int os::posix::io::fstat ( struct stat buf)
virtualinherited

Reimplemented in os::posix::file_lockable< T, L >.

Definition at line 416 of file io.cpp.

417 {
418#if defined(OS_TRACE_POSIX_IO_IO)
419 trace::printf ("io::%s(%p) @%p\n", __func__, buf, this);
420#endif
421
422 if (buf == nullptr)
423 {
424 errno = EFAULT;
425 return -1;
426 }
427
428 if (!impl ().do_is_opened ())
429 {
430 errno = EBADF; // Not opened.
431 return -1;
432 }
433
434 if (!impl ().do_is_connected ())
435 {
436 errno = EIO; // Not opened.
437 return -1;
438 }
439
440 errno = 0;
441
442 // Execute the implementation specific code.
443 return impl ().do_fstat (buf);
444 }
virtual int do_fstat(struct stat *buf)
Definition io.cpp:539

References os::posix::io_impl::do_fstat(), os::posix::io::impl(), and os::trace::printf().

Referenced by os::posix::file_lockable< T, L >::fstat().

◆ get_type()

io::type_t os::posix::io::get_type ( void  ) const
inlineinherited

Definition at line 434 of file io.h.

435 {
436 return type_;
437 }

Referenced by os::posix::file_descriptors_manager::socket().

◆ impl()

template<typename T , typename L >
block_device_lockable< T, L >::value_type & os::posix::block_device_lockable< T, L >::impl ( void  ) const

Definition at line 686 of file block-device.h.

687 {
688 return static_cast<value_type&> (impl_);
689 }

◆ ioctl()

int os::posix::device::ioctl ( int  request,
  ... 
)
inherited

Definition at line 135 of file device.cpp.

136 {
137 // Forward to the variadic version of the function.
138 std::va_list args;
139 va_start (args, request);
140 int ret = vioctl (request, args);
141 va_end (args);
142
143 return ret;
144 }
virtual int vioctl(int request, std::va_list args)
Definition device.cpp:152

References os::posix::device::vioctl().

◆ is_opened()

bool os::posix::io::is_opened ( void  )
inlineinherited

Definition at line 458 of file io.h.

459 {
460 return impl ().do_is_opened ();
461 }
virtual bool do_is_opened(void)=0

References os::posix::io_impl::do_is_opened(), and os::posix::io::impl().

◆ isatty()

int os::posix::io::isatty ( void  )
inherited

Definition at line 406 of file io.cpp.

407 {
408 errno = 0;
409
410 // Execute the implementation specific code.
411 return impl ().do_isatty ();
412 }
virtual int do_isatty(void)
Definition io.cpp:532

References os::posix::io_impl::do_isatty(), and os::posix::io::impl().

◆ lseek()

template<typename T , typename L >
off_t os::posix::block_device_lockable< T, L >::lseek ( off_t  offset,
int  whence 
)
overridevirtual

Reimplemented from os::posix::io.

Definition at line 629 of file block-device.h.

630 {
631#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE)
632 trace::printf ("block_device_lockable::%s(%d, %d) @%p\n", __func__,
633 offset, whence, this);
634#endif
635
636 std::lock_guard<L> lock{ locker_ };
637
638 return block_device::lseek (offset, whence);
639 }
virtual off_t lseek(off_t offset, int whence)
Definition io.cpp:447

References os::posix::io::lseek(), and os::trace::printf().

◆ match_name()

bool os::posix::device::match_name ( const char *  name) const
virtualinherited

Definition at line 189 of file device.cpp.

190 {
191 assert (name != nullptr);
192 assert (name_ != nullptr);
193
194 return (std::strcmp (name, name_) == 0);
195 }

References os::posix::device::name().

◆ name()

const char * os::posix::device::name ( void  ) const
inlineinherited

Definition at line 304 of file device.h.

305 {
306 return name_;
307 }

Referenced by os::posix::device_registry< T >::link(), and os::posix::device::match_name().

◆ open()

int os::posix::device::open ( const char *  path = nullptr,
int  oflag = 0,
  ... 
)
inherited
Parameters
pathMay be nullptr.
oflagOutput flags
Returns
non-negative file descriptor, -1 on failure

Definition at line 62 of file device.cpp.

63 {
64 // Forward to the variadic version of the function.
65 std::va_list args;
66 va_start (args, oflag);
67 int ret = vopen (path, oflag, args);
68 va_end (args);
69
70 return ret;
71 }
int vopen(const char *path, int oflag, std::va_list args)
Definition device.cpp:74

References os::posix::device::vopen().

◆ read()

template<typename T , typename L >
ssize_t os::posix::block_device_lockable< T, L >::read ( void *  buf,
std::size_t  nbyte 
)
overridevirtual

Reimplemented from os::posix::io.

Definition at line 558 of file block-device.h.

559 {
560#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE)
561 trace::printf ("block_device_lockable::%s(0x0%X, %u) @%p\n", __func__,
562 buf, nbyte, this);
563#endif
564
565 std::lock_guard<L> lock{ locker_ };
566
567 return block_device::read (buf, nbyte);
568 }
virtual ssize_t read(void *buf, std::size_t nbyte)
Definition io.cpp:217

References os::trace::printf(), and os::posix::io::read().

◆ read_block()

template<typename T , typename L >
ssize_t os::posix::block_device_lockable< T, L >::read_block ( void *  buf,
blknum_t  blknum,
std::size_t  nblocks = 1 
)
overridevirtual

Reimplemented from os::posix::block_device.

Definition at line 643 of file block-device.h.

645 {
646#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE)
647 trace::printf ("block_device_lockable::%s(%p, %u, %u) @%p\n", __func__,
648 buf, blknum, nblocks, this);
649#endif
650
651 std::lock_guard<L> lock{ locker_ };
652
653 return block_device::read_block (buf, blknum, nblocks);
654 }
virtual ssize_t read_block(void *buf, blknum_t blknum, std::size_t nblocks=1)

References os::trace::printf(), and os::posix::block_device::read_block().

◆ sync()

template<typename T , typename L >
void os::posix::block_device_lockable< T, L >::sync ( void  )
overridevirtual

Reimplemented from os::posix::device.

Definition at line 673 of file block-device.h.

674 {
675#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE)
676 trace::printf ("block_device_lockable::%s() @%p\n", __func__, this);
677#endif
678
679 std::lock_guard<L> lock{ locker_ };
680
681 return block_device::sync ();
682 }
virtual void sync(void)
Definition device.cpp:171

References os::trace::printf(), and os::posix::device::sync().

◆ vfcntl()

template<typename T , typename L >
int os::posix::block_device_lockable< T, L >::vfcntl ( int  cmd,
std::va_list  args 
)
overridevirtual

Reimplemented from os::posix::io.

Definition at line 601 of file block-device.h.

602 {
603#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE)
604 trace::printf ("block_device_lockable::%s(%d) @%p\n", __func__, cmd,
605 this);
606#endif
607
608 std::lock_guard<L> lock{ locker_ };
609
610 return block_device::vfcntl (cmd, args);
611 }

References os::trace::printf(), and os::posix::io::vfcntl().

◆ vioctl()

template<typename T , typename L >
int os::posix::block_device_lockable< T, L >::vioctl ( int  request,
std::va_list  args 
)
overridevirtual

Reimplemented from os::posix::block_device.

Definition at line 615 of file block-device.h.

616 {
617#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE)
618 trace::printf ("block_device_lockable::%s(%d) @%p\n", __func__, request,
619 this);
620#endif
621
622 std::lock_guard<L> lock{ locker_ };
623
624 return block_device::vioctl (request, args);
625 }
virtual int vioctl(int request, std::va_list args) override

References os::trace::printf(), and os::posix::block_device::vioctl().

◆ vopen()

int os::posix::device::vopen ( const char *  path,
int  oflag,
std::va_list  args 
)
inherited
Parameters
pathMay be nullptr.
oflagOutput flags
argsArguments
Returns
non-negative file descriptor, -1 on failure

Definition at line 74 of file device.cpp.

75 {
76#if defined(OS_TRACE_POSIX_IO_DEVICE)
77 trace::printf ("device::%s(\"%s\") @%p\n", __func__, path ? path : "",
78 this);
79#endif
80
81 errno = 0;
82
83 int ret = 0;
84 if (impl ().open_count_ == 0)
85 {
86 // If so, use the implementation to open the device.
87 ret = impl ().do_vopen (path, oflag, args);
88 if (ret < 0)
89 {
90 // Open failed.
91 return -1;
92 }
93
94 auto iop = alloc_file_descriptor ();
95 if (iop == nullptr)
96 {
97 return -1;
98 }
99 }
100 ++(impl ().open_count_);
101 ret = file_descriptor ();
102#if defined(OS_TRACE_POSIX_IO_DEVICE)
103 trace::printf ("device::%s(\"%s\")=%p fd=%d\n", __func__,
104 path ? path : "", this, ret);
105#endif
106
107 return ret;
108 }
virtual int do_vopen(const char *path, int oflag, std::va_list args)=0
device_impl & impl(void) const
Definition device.h:310
file_descriptor_t file_descriptor(void) const
Definition io.h:452
io * alloc_file_descriptor(void)
Definition io.cpp:189

References os::posix::io::alloc_file_descriptor(), os::posix::device_impl::do_vopen(), os::posix::io::file_descriptor(), os::posix::device::impl(), and os::trace::printf().

Referenced by os::posix::device::open().

◆ write()

template<typename T , typename L >
ssize_t os::posix::block_device_lockable< T, L >::write ( const void *  buf,
std::size_t  nbyte 
)
overridevirtual

Reimplemented from os::posix::io.

Definition at line 572 of file block-device.h.

573 {
574#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE)
575 trace::printf ("block_device_lockable::%s(0x0%X, %u) @%p\n", __func__,
576 buf, nbyte, this);
577#endif
578
579 std::lock_guard<L> lock{ locker_ };
580
581 return block_device::write (buf, nbyte);
582 }
virtual ssize_t write(const void *buf, std::size_t nbyte)
Definition io.cpp:268

References os::trace::printf(), and os::posix::io::write().

◆ write_block()

template<typename T , typename L >
ssize_t os::posix::block_device_lockable< T, L >::write_block ( const void *  buf,
blknum_t  blknum,
std::size_t  nblocks = 1 
)
overridevirtual

Reimplemented from os::posix::block_device.

Definition at line 658 of file block-device.h.

660 {
661#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE)
662 trace::printf ("block_device_lockable::%s(%p, %u, %u) @%p\n", __func__,
663 buf, blknum, nblocks, this);
664#endif
665
666 std::lock_guard<L> lock{ locker_ };
667
668 return block_device::write_block (buf, blknum, nblocks);
669 }
virtual ssize_t write_block(const void *buf, blknum_t blknum, std::size_t nblocks=1)

References os::trace::printf(), and os::posix::block_device::write_block().

◆ writev()

template<typename T , typename L >
ssize_t os::posix::block_device_lockable< T, L >::writev ( const iovec iov,
int  iovcnt 
)
overridevirtual

Reimplemented from os::posix::io.

Definition at line 586 of file block-device.h.

588 {
589#if defined(OS_TRACE_POSIX_IO_BLOCK_DEVICE)
590 trace::printf ("block_device_lockable::%s(0x0%X, %d) @%p\n", __func__,
591 iov, iovcnt, this);
592#endif
593
594 std::lock_guard<L> lock{ locker_ };
595
596 return block_device::writev (iov, iovcnt);
597 }
virtual ssize_t writev(const iovec *iov, int iovcnt)
Definition io.cpp:321

References os::trace::printf(), and os::posix::io::writev().


The documentation for this class was generated from the following file: