]> git.tdb.fi Git - libs/core.git/commitdiff
Merge agouti:prog/core
authorMikko Rasa <tdb@tdb.fi>
Wed, 27 Jul 2016 15:49:16 +0000 (18:49 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 27 Jul 2016 15:49:16 +0000 (18:49 +0300)
24 files changed:
Build
source/core/android/mainthread.cpp
source/core/android/mainthread.h
source/core/application.h
source/core/mutex.h
source/core/thread.cpp
source/core/thread.h
source/core/unix/thread.cpp
source/core/windows/thread.cpp
source/debug/backtrace.cpp
source/fs/dir.cpp
source/fs/osx/cfdir.m [new file with mode: 0644]
source/fs/osx/dir_location.cpp [new file with mode: 0644]
source/fs/path.cpp
source/fs/path.h
source/fs/stat_private.h
source/io/base.h
source/io/file.cpp
source/io/poll.cpp
source/io/unix/seekable.cpp
source/time/datetime.cpp
source/time/datetime.h
source/time/timer.cpp
source/time/timer.h

diff --git a/Build b/Build
index d940f4a695ce976c886be6341c08d85ed9a5bade..12e3114909efbd16356a498602d166db710ddf1e 100644 (file)
--- a/Build
+++ b/Build
@@ -24,6 +24,14 @@ package "mspcore"
                };
        };
 
+       if_arch "darwin"
+       {
+               build_info
+               {
+                       library "Foundation.framework";
+               };
+       };
+
        feature "zlib" "Support compression with zlib"
        {
                default "yes";
index 255eaa3260b892f43ae5d69eb670340b6a487120..8ce484a0dee75e4a014a7440d2775c891fbe18a3 100644 (file)
@@ -51,6 +51,14 @@ void MainThread::resume_startup()
        }
 }
 
+JavaVM *MainThread::get_java_vm() const
+{
+       if(!activity)
+               return 0;
+
+       return activity->vm;
+}
+
 void MainThread::set_window_flags(unsigned set, unsigned clear)
 {
        ANativeActivity_setWindowFlags(activity, set, clear);
index 22309eee3d3d2ca47584573210ebb7a8935f7234..609300fb85a8a4db64e680c9f4dfd7f754d78bc4 100644 (file)
@@ -34,6 +34,7 @@ public:
        void wait_for_app_created();
        void resume_startup();
 
+       JavaVM *get_java_vm() const;
        AAssetManager *get_asset_manager() const { return asset_manager; }
        const FS::Path &get_internal_data_path() const { return int_data_path; }
 
index 6b3ec1aa65bdd98b2877440a3f7a2f6e668d191a..d0985be900fb493714760ab07335d06faaa87298 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_CORE_APPLICATION_H_
 
 #include <stdexcept>
+#include <string>
 
 namespace Msp {
 
index 0fe9e82087ff67d4c51d1d7bcde0f2a10d668138..71fbdf0c6e5796423274049dc28a05dfba42cbb2 100644 (file)
@@ -18,6 +18,8 @@ private:
 
        Private *priv;
 
+       Mutex(const Mutex &);
+       Mutex &operator=(const Mutex &);
 public:
        Mutex();
        ~Mutex();
index d2451a0d819187e67f0ea41340720e62ac7f3a4a..721e8cfef51f2ff772c304ff9ece65554c3f033a 100644 (file)
@@ -6,8 +6,9 @@ using namespace std;
 
 namespace Msp {
 
-Thread::Thread():
+Thread::Thread(const string &name):
        priv_(new Private),
+       name_(name),
        state_(PENDING)
 { }
 
@@ -48,6 +49,7 @@ void Thread::launch()
 ThreadReturn THREAD_CALL Thread::Private::main_wrapper(void *arg)
 {
        Thread *thread = reinterpret_cast<Thread *>(arg);
+       thread->platform_setname();
        thread->main();
        thread->state_ = FINISHED;
        return 0;
index 1fff3627b037287aac89cb8e68e77f15d5543b47..7cf17b8d327ed3e84075bb4c247f4c5d16c4ff6f 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef MSP_CORE_THREAD_H_
 #define MSP_CORE_THREAD_H_
 
+#include <string>
+
 namespace Msp {
 
 /**
@@ -25,16 +27,19 @@ private:
        };
 
        Private *priv_;
+       std::string name_;
        State state_;
 
 protected:
-       Thread();
+       Thread(const std::string & = std::string());
 private:
        Thread(const Thread &);
        Thread &operator=(const Thread &);
 public:
        virtual ~Thread();
 
+       const std::string &get_name() const { return name_; }
+
        /** Indicates whether the thread has finished running. */
        bool is_finished() { return state_>=FINISHED; }
 
@@ -54,6 +59,7 @@ private:
        void platform_join();
        void platform_kill();
        void platform_launch();
+       void platform_setname();
 
 protected:
        virtual void main() = 0;
index 26afeefc4300e0955b52ca72931c1c2b63eb0d16..3c17a9aabc8054c423253c115ba803325dc3de62 100644 (file)
@@ -20,4 +20,10 @@ void Thread::platform_launch()
        pthread_create(&priv_->handle, 0, &Private::main_wrapper, this);
 }
 
+void Thread::platform_setname()
+{
+       if(!name_.empty())
+               pthread_setname_np(priv_->handle, name_.c_str());
+}
+
 } // namespace Msp
index 7c436183d4f279cdc2a034053041fb5c5b96ad1d..ab02c4d04094394e384b91defd717b70ac5918df 100644 (file)
@@ -20,4 +20,9 @@ void Thread::platform_launch()
        priv_->handle = CreateThread(0, 0, &Private::main_wrapper, this, 0, &dummy);
 }
 
+void Thread::platform_setname()
+{
+       // TODO: https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
+}
+
 } // namespace Msp
index 1fda155e9665d497f629cb575ea93a33b12a7d3b..f9057dce833d95b9231917bd763a489f7a856fb1 100644 (file)
@@ -1,6 +1,6 @@
 // Must include something to test for glibc
 #include <cstdlib>
-#if !defined(WIN32) && defined(__GLIBC__)
+#if !defined(_WIN32) && defined(__GLIBC__)
 #include <dlfcn.h>
 #include <execinfo.h>
 #endif
@@ -14,7 +14,7 @@ namespace Debug {
 
 Backtrace Backtrace::create()
 {
-#if !defined(WIN32) && defined(__GLIBC__)
+#if !defined(_WIN32) && defined(__GLIBC__)
        void *addresses[50];
        int count = ::backtrace(addresses, 50);
 
index 9c6a97a8318ceaccb77c2d708f7ae65a45f3d951..717c11c132d7e4fe6e99995827e4918d443062fb 100644 (file)
@@ -20,7 +20,7 @@ namespace
 
 enum
 {
-#ifdef WIN32
+#ifdef _WIN32
        ITEMSEP = ';'
 #else
        ITEMSEP = ':'
@@ -73,7 +73,7 @@ void mkpath(const Path &path, int mode)
        for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
        {
                p /= *i;
-#ifdef WIN32
+#ifdef _WIN32
                if(p.size()==1 && p.is_absolute())
                        continue;
 #endif
diff --git a/source/fs/osx/cfdir.m b/source/fs/osx/cfdir.m
new file mode 100644 (file)
index 0000000..4dd4b4b
--- /dev/null
@@ -0,0 +1,23 @@
+#import <Foundation/NSPathUtilities.h>
+
+unsigned get_home_dir(char *buf, unsigned size)
+{
+       NSString *path = NSHomeDirectory();
+       if(![path getCString:buf maxLength:size encoding:NSUTF8StringEncoding])
+               return 0;
+
+       return [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
+}
+
+unsigned get_application_support_dir(char *buf, unsigned size)
+{
+       NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
+       if(![dirs count])
+               return 0;
+
+       NSString *path = [dirs objectAtIndex:0];
+       if(![path getCString:buf maxLength:size encoding:NSUTF8StringEncoding])
+               return 0;
+
+       return [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
+}
diff --git a/source/fs/osx/dir_location.cpp b/source/fs/osx/dir_location.cpp
new file mode 100644 (file)
index 0000000..7ce89f7
--- /dev/null
@@ -0,0 +1,35 @@
+#include "dir.h"
+
+using namespace std;
+
+extern "C" unsigned get_home_dir(char *, unsigned);
+extern "C" unsigned get_application_support_dir(char *, unsigned);
+
+namespace Msp {
+namespace FS {
+
+Path get_home_dir()
+{
+       char buf[1024];
+       unsigned len = ::get_home_dir(buf, sizeof(buf));
+       if(len)
+               return string(buf, len);
+
+       const char *home = getenv("HOME");
+       if(home)
+               return home;
+
+       return ".";
+}
+
+Path get_user_data_dir(const string &appname)
+{
+       char buf[1024];
+       unsigned len = get_application_support_dir(buf, sizeof(buf));
+       if(len)
+               return Path(string(buf, len))/appname;
+       return get_home_dir()/"Library"/"Application Support"/appname;
+}
+
+} // namespace FS
+} // namespace Msp
index 0fe95d65df943a9f0b4b1b4bf006035a4e7dccee..d68595bb55ee83588ce9ae7a535c9424cbbaabef 100644 (file)
@@ -7,7 +7,7 @@ using namespace std;
 
 namespace {
 
-#ifdef WIN32
+#ifdef _WIN32
 inline bool is_windows_drive(const std::string &p)
 { return (p.size()==2 && ((p[0]>='A' && p[0]<='Z') || (p[0]>='a' && p[0]<='z')) && p[1]==':'); }
 #endif
@@ -58,7 +58,7 @@ unsigned Path::size() const
 
 bool Path::is_absolute() const
 {
-#ifdef WIN32
+#ifdef _WIN32
        if(is_windows_drive((*this)[0]))
                return true;
 #endif
@@ -103,7 +103,7 @@ void Path::add_component(const string &comp)
        if(comp.size()==1 && (comp[0]=='/' || comp[0]=='\\'))
        {
                // Replace the path with the root directory
-#ifdef WIN32
+#ifdef _WIN32
                string::size_type slash = (separators.empty() ? string::npos : separators.front());
                if(is_windows_drive(path.substr(0, slash)))
                {
@@ -118,7 +118,7 @@ void Path::add_component(const string &comp)
                        separators.push_back(0);
                }
        }
-#ifdef WIN32
+#ifdef _WIN32
        else if(is_windows_drive(comp))
        {
                path = comp;
@@ -132,7 +132,7 @@ void Path::add_component(const string &comp)
                // .. in root directory is a no-op
                else if(path.size()==1 && path[0]==DIRSEP)
                        ;
-#ifdef WIN32
+#ifdef _WIN32
                else if(is_windows_drive(path))
                        ;
 #endif
@@ -192,7 +192,7 @@ string Path::operator[](int n) const
 
 bool Path::operator==(const Path &other) const
 {
-#ifdef WIN32
+#ifdef _WIN32
        return strcasecmp(path, other.path)==0;
 #else
        return path==other.path;
@@ -201,7 +201,7 @@ bool Path::operator==(const Path &other) const
 
 bool Path::operator<(const Path &other) const
 {
-#ifdef WIN32
+#ifdef _WIN32
        return strcasecmp(path, other.path)<0;
 #else
        return path<other.path;
@@ -210,7 +210,7 @@ bool Path::operator<(const Path &other) const
 
 bool Path::operator>(const Path &other) const
 {
-#ifdef WIN32
+#ifdef _WIN32
        return strcasecmp(path, other.path)>0;
 #else
        return path>other.path;
index 3cbfb16cf6e4052c57d223cd1fafd96b377b1c98..82600dc35c28d5787f69b56f0af3b8b12af50f22 100644 (file)
@@ -10,7 +10,7 @@ namespace FS {
 
 enum
 {
-#ifdef WIN32
+#ifdef _WIN32
        DIRSEP = '\\'
 #else
        DIRSEP = '/'
index c80d15119b1e2ffb1e6a0a5ebb27486ec8963031..7b20638d36ed5b5a1e75d2f14ce9fb102dfc44c0 100644 (file)
@@ -15,7 +15,7 @@ struct Stat::Private
        Private(const Private &);
        ~Private();
 
-#ifndef WIN32
+#ifndef _WIN32
        /* This is here because it needs access to private members of Stat, but we
        can't expose the system stat struct in the public header */
        static Stat from_struct_stat(const struct stat &);
index c58be90f7ebd039504e261105e53dc7329a1ac13..8d29caffd9813096747b1c2822f6e1b185b1d854 100644 (file)
@@ -12,9 +12,6 @@ namespace IO {
 
 /**
 Common interface for all I/O objects.
-
-A derived class must call set_events(P_NONE) before it is destroyed to avoid
-leaving stale pointers in an EventDispatcher.
 */
 class Base
 {
index 666b1edc784b9fc9d0bc76cf29d77b82ff80b826..1b4a73884d7b5891a43c461d79901bd7e9d1a147 100644 (file)
@@ -39,7 +39,7 @@ unsigned File::do_write(const char *buf, unsigned size)
        if(size==0)
                return 0;
 
-#ifdef WIN32
+#ifdef _WIN32
        if(mode&M_APPEND)
                seek(0, S_END);
 #endif
index 0356a00fdbe27ffe957f340fb25d82b5cf1d2c7a..6da777cf4470778f4e6401dbcc39f1a3a539b4e7 100644 (file)
@@ -36,7 +36,7 @@ void Poller::set_object(EventObject &obj, PollEvent ev)
        }
        else if(ev)
        {
-#ifdef WIN32
+#ifdef _WIN32
                if(objects.size()>=MAXIMUM_WAIT_OBJECTS)
                        throw logic_error("Maximum number of wait objects reached");
 #endif
index 93a6e86e1f8427635f5f7da38ac825e7c8fe3a3f..a402742a52338b7ba2d5f17cd639d6b37ca18c84 100644 (file)
@@ -29,6 +29,13 @@ int sys_seek_type(SeekType st)
 namespace Msp {
 namespace IO {
 
+/* Android libc does not recognize _FILE_OFFSET_BITS so this hack is necessary
+to get 64-bit seeks. */
+#ifdef __ANDROID__
+#define off_t off64_t
+#define lseek lseek64
+#endif
+
 SeekOffset sys_seek(Handle &handle, SeekOffset offset, SeekType type)
 {
        off_t ret = lseek(*handle, offset, sys_seek_type(type));
index c77796f7bc0bd8c8cd378d739fb5a458f006a491..26bd7cdb7d59925a0caf076b22dab059e8498a60 100644 (file)
@@ -1,6 +1,7 @@
 #include <cstdlib>
 #include <stdexcept>
 #include <msp/strings/format.h>
+#include <msp/strings/regex.h>
 #include "datetime.h"
 #include "timestamp.h"
 
@@ -104,6 +105,36 @@ void DateTime::init(int y, unsigned char m, unsigned char d, unsigned char h, un
                throw out_of_range("DateTime::DateTime mday");
 }
 
+DateTime DateTime::parse_rfc3339(const string &str)
+{
+       static Regex re("^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[-+]([0-9]{2}):([0-9]{2}))$");
+
+       RegMatch m = re.match(str);
+       if(!m)
+               throw invalid_argument("DateTime::parse_rfc3339");
+
+       unsigned year = lexical_cast<unsigned>(m[1].str);
+       unsigned month = lexical_cast<unsigned>(m[2].str);
+       unsigned mday = lexical_cast<unsigned>(m[3].str);
+       unsigned hr = lexical_cast<unsigned>(m[4].str);
+       unsigned minute = lexical_cast<unsigned>(m[5].str);
+       unsigned second = lexical_cast<unsigned>(m[6].str);
+
+       DateTime result = DateTime(year, month, mday, hr, minute, second);
+
+       int tzoff = 0;
+       if(m[7].str!="Z")
+       {
+               tzoff = lexical_cast<unsigned>(m[8].str)*60+lexical_cast<unsigned>(m[9].str);
+               if(m[7].str[0]=='-')
+                       tzoff = -tzoff;
+       }
+
+       result.set_timezone(tzoff);
+
+       return result;
+}
+
 void DateTime::add_days(int days)
 {
        int new_year = year;
index f065d3219e7f679d3741bf03dad4f2680d06a30d..b7f578cd9f71326ae510c0780c8714f195aa684d 100644 (file)
@@ -43,6 +43,8 @@ private:
        void init(int, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned);
 
 public:
+       static DateTime parse_rfc3339(const std::string &);
+
        int get_year() const { return year; }
        unsigned char get_month() const { return month; }
        unsigned char get_mday() const { return mday; }
@@ -80,6 +82,9 @@ private:
 inline void operator<<(LexicalConverter &c, const DateTime &d)
 { c.result(d.format_rfc3339()); }
 
+inline void operator>>(const LexicalConverter &c, DateTime &d)
+{ d = DateTime::parse_rfc3339(c.get()); }
+
 } // namespace Time
 } // namespace Msp
 
index f26900b8d4a589769c707b59513de6aa2d8dc129..e0af57cc78fcc41363632e169e33a8219653e9f1 100644 (file)
@@ -1,4 +1,5 @@
 #include <algorithm>
+#include <msp/core/raii.h>
 #include "timer.h"
 #include "utils.h"
 
@@ -55,35 +56,59 @@ void Timer::cancel(Slot &slot)
 
 void Timer::tick(bool block)
 {
+       if(block)
+               tick();
+       else
+               tick(zero);
+}
+
+void Timer::tick()
+{
+       do_tick(-sec);
+}
+
+void Timer::tick(const TimeDelta &timeout)
+{
+       if(timeout<zero)
+               throw invalid_argument("Timer::tick");
+
+       do_tick(timeout);
+}
+
+void Timer::do_tick(const TimeDelta &timeout)
+{
+       TimeStamp deadline;
+       if(timeout>=zero)
+               deadline = now()+timeout;
+
        Slot *next = 0;
        {
                MutexLock l(mutex);
                while(1)
                {
-                       if(slots.empty())
+                       TimeStamp stamp;
+                       TimeStamp t = now();
+                       if(!slots.empty())
                        {
-                               if(block)
-                               {
-                                       blocking = true;
-                                       mutex.unlock();
-                                       sem.wait();
-                                       mutex.lock();
-                               }
-                               else
-                                       return;
+                               next = slots.begin()->slot;
+                               stamp = next->get_timeout();
+                               if(stamp<=t)
+                                       break;
                        }
 
-                       next = slots.begin()->slot;
-                       const TimeStamp &stamp = next->get_timeout();
-                       const TimeStamp t = now();
-                       if(stamp<=t)
-                               break;
-                       else if(block)
+                       if(timeout && (!deadline || t<deadline))
                        {
-                               blocking = true;
+                               SetFlag setf(blocking);
                                mutex.unlock();
-                               sem.wait(stamp-t);
+                               if(stamp && (!deadline || stamp<deadline))
+                                       sem.wait(stamp-t);
+                               else if(deadline)
+                                       sem.wait(deadline-t);
+                               else
+                                       sem.wait();
                                mutex.lock();
+                               // The slots may have changed while waiting so check again
+                               continue;
                        }
                        else
                                return;
index 90d870ca9a3afbd44b155f5ccfb5b85747c02442..21aeb2b4b5b02ea0b41932f4a3311028f35c7e55 100644 (file)
@@ -66,13 +66,21 @@ public:
        /** Cancels a previously added timer. */
        void cancel(Slot &);
 
-       /** Checks all timers, executing any that have timed out.  If block is true,
-       waits until one times out.
+       /** Deprecated.  Use one of the other overloads. */
+       void tick(bool block);
 
-       Note: If there are no active timers when a blocking tick is executed, it
-       won't return until a timer is added from another thread. */
-       void tick(bool block = true);
+       /** Waits until a timer expires, then executes it.  If no timers have been
+       set, blocks until one is added from another thread. */
+       void tick();
 
+       /** Waits until a timer expires but at most the specified amount of time.
+       If a timer did expire before the timeout, it is executed. */
+       void tick(const TimeDelta &);
+
+private:
+       void do_tick(const TimeDelta &);
+
+public:
        TimeStamp get_next_timeout() const;
 };