예제 #1
0
    void threaded_object::
    restart (
    )
    {
        auto_mutex M(m_);

        DLIB_ASSERT(id1 != get_thread_id() || id_valid == false,
               "\tvoid threaded_object::restart()"
               << "\n\tYou can NOT call this function from the thread that executes threaded_object::thread"
               << "\n\tthis: " << this
        );

        if (is_alive_ == false)
        {
            if (create_new_thread<threaded_object,&threaded_object::thread_helper>(*this) == false)
            {
                is_running_ = false;
                throw thread_error();
            }
            should_respawn_ = false;
        }
        else
        {
            should_respawn_ = true;
        }
        is_alive_ = true;
        is_running_ = true;
        should_stop_ = false;
        s.broadcast();
    }
예제 #2
0
        std::streamsize xsputn (
            const char* s,
            std::streamsize num
        )
        {
            // Add a sanity check here 
            DLIB_ASSERT(num >= 0,
                "\tstd::streamsize filestreambuf::xsputn"
                << "\n\tThe number of bytes to write can't be negative"
                << "\n\tnum:  " << num 
                << "\n\tthis: " << this
            );

            std::streamsize space_left = static_cast<std::streamsize>(epptr()-pptr());
            if (num <= space_left)
            {
                std::memcpy(pptr(),s,static_cast<size_t>(num));
                pbump(static_cast<int>(num));
                return num;
            }
            else
            {
                std::memcpy(pptr(),s,static_cast<size_t>(space_left));
                s += space_left;
                pbump(space_left);
                std::streamsize num_left = num - space_left;

                if (flush_out_buffer() == EOF)
                {
                    // the write was not successful so return that 0 bytes were written
                    return 0;
                }

                if (num_left < out_buffer_size)
                {
                    std::memcpy(pptr(),s,static_cast<size_t>(num_left));
                    pbump(num_left);
                    return num;
                }
                else
                {
                    while(num_left != 0)
                    {
                        if(write_echoing_select(fd, fd_printf))
                            return EOF;
                        int status = write(fd,s,num_left);
                        if (status < 0)
                        {
                            // the write was not successful so return that 0 bytes were written
                            return 0;
                        } 
                        num_left -= status;
                        s += status;
                    }
                    return num;
                }
            }
        }
 multithreaded_object::
 ~multithreaded_object (
 )
 {
     DLIB_ASSERT(number_of_threads_alive() == 0,
            "\tmultithreaded_object::~multithreaded_object()"
            << "\n\tYou have let a multithreaded object destruct itself before terminating its threads"
            << "\n\tthis: " << this
     );
 }
예제 #4
0
 drawable::
 ~drawable (
 )
 {
     DLIB_ASSERT(events_are_enabled() == false,
         "\tdrawable::~drawable()"
         << "\n\tYou must disable events for drawable objects in their destructor by calling disable_events()."
         << "\n\tthis:     " << this
         );
     disable_events();
 }
    std::streamsize sockstreambuf_kernel_2::
    xsputn (
        const char* s,
        std::streamsize num
    )
    {
        // Add a sanity check here 
        DLIB_ASSERT(num >= 0,
            "\tstd::streamsize sockstreambuf::xsputn"
            << "\n\tThe number of bytes to write can't be negative"
            << "\n\tnum:  " << num 
            << "\n\tthis: " << this
            );

        std::streamsize space_left = static_cast<std::streamsize>(epptr()-pptr());
        if (num <= space_left)
        {
            std::memcpy(pptr(),s,static_cast<size_t>(num));
            pbump(static_cast<int>(num));
            return num;
        }
        else
        {
            std::memcpy(pptr(),s,static_cast<size_t>(space_left));
            s += space_left;
            pbump(space_left);
            std::streamsize num_left = num - space_left;

            if (flush_out_buffer() == EOF)
            {
                // the write was not successful so return that 0 bytes were written
                return 0;
            }

            if (num_left < out_buffer_size)
            {
                std::memcpy(pptr(),s,static_cast<size_t>(num_left));
                pbump(num_left);
                return num;
            }
            else
            {
                if (con.write(s,num_left) != num_left)
                {
                    // the write was not successful so return that 0 bytes were written
                    return 0;
                } 
                return num;
            }
        }
    }
 bool multithreaded_object::
 should_stop (
 ) const
 {
     auto_mutex M(m);
     DLIB_ASSERT(thread_ids.is_in_domain(get_thread_id()),
            "\tbool multithreaded_object::should_stop()"
            << "\n\tYou can only call this function from one of the registered threads in this object"
            << "\n\tthis: " << this
     );
     while (is_running_ == false && should_stop_ == false)
         s.wait();
     return should_stop_;
 }
예제 #7
0
    bool threaded_object::
    is_alive (
    ) const
    {
        auto_mutex M(m_);

        DLIB_ASSERT(id1 != get_thread_id() || id_valid == false,
               "\tbool threaded_object::is_alive()"
               << "\n\tYou can NOT call this function from the thread that executes threaded_object::thread"
               << "\n\tthis: " << this
        );

        return is_alive_;
    }
예제 #8
0
 bool threaded_object::
 should_stop (
 ) const
 {
     auto_mutex M(m_);
     DLIB_ASSERT(is_alive_ && id1 == get_thread_id() && id_valid == true,
            "\tbool threaded_object::should_stop()"
            << "\n\tYou can only call this function from the thread that executes threaded_object::thread"
            << "\n\tthis: " << this
     );
     while (is_running_ == false && should_stop_ == false)
         s.wait();
     return should_stop_;
 }
예제 #9
0
    void threaded_object::
    pause (
    )
    {
        auto_mutex M(m_);

        DLIB_ASSERT(id1 != get_thread_id() || id_valid == false,
               "\tvoid threaded_object::pause()"
               << "\n\tYou can NOT call this function from the thread that executes threaded_object::thread"
               << "\n\tthis: " << this
        );

        is_running_ = false;
    }
예제 #10
0
    void threaded_object::
    set_respawn (
    )
    {
        auto_mutex M(m_);

        DLIB_ASSERT(id1 != get_thread_id() || id_valid == false,
               "\tvoid threaded_object::set_respawn()"
               << "\n\tYou can NOT call this function from the thread that executes threaded_object::thread"
               << "\n\tthis: " << this
        );

        should_respawn_ = true;
    }
    void multithreaded_object::
    wait (
    ) const
    {
        auto_mutex M(m);

        DLIB_ASSERT(thread_ids.is_in_domain(get_thread_id()) == false,
               "\tvoid multithreaded_object::wait()"
               << "\n\tYou can NOT call this function from one of the threads registered in this object"
               << "\n\tthis: " << this
        );

        while (threads_started > 0)
            s.wait();
    }
예제 #12
0
    void threaded_object::
    wait (
    ) const
    {
        auto_mutex M(m_);

        DLIB_ASSERT(id1 != get_thread_id() || id_valid == false,
               "\tvoid threaded_object::wait()"
               << "\n\tYou can NOT call this function from the thread that executes threaded_object::thread"
               << "\n\tthis: " << this
        );

        while (is_alive_)
            s.wait();
    }
예제 #13
0
    void threaded_object::
    stop (
    )
    {
        auto_mutex M(m_);

        DLIB_ASSERT(id1 != get_thread_id() || id_valid == false,
               "\tvoid threaded_object::stop()"
               << "\n\tYou can NOT call this function from the thread that executes threaded_object::thread"
               << "\n\tthis: " << this
        );

        should_stop_ = true;
        is_running_ = false;
        should_respawn_ = false;
        s.broadcast();
    }
예제 #14
0
 threaded_object::
 ~threaded_object (
 )
 {
     try
     {
         DLIB_ASSERT(is_alive() == false,
                "\tthreaded_object::~threaded_object()"
                << "\n\tYou have let a threaded object destruct itself before terminating its thread"
                << "\n\tthis: " << this
         );
     }
     catch (std::exception& e)
     {
         std::cerr << e.what() << std::endl;
         assert(false);
         abort();
     }
 }