From f2b66913326b7480559ad0cacaaaf6282603b0fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20L=C3=B8vdal?= Date: Sun, 17 Feb 2019 18:02:42 +0100 Subject: [PATCH] Use uintptr_t type for ThreadID MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For Arduino target code there is no difference between int and pointer sizes, e.g. $HOME/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino4/avr/include/stdint.h typedef uint16_t uintptr_t; but it matters when compiling the code for unit test on a 64bit linux host computer: .../arduino/sketchbook/libraries/ArduinoThread/Thread.cpp: In constructor ‘Thread::Thread(void (*)(), long unsigned int)’: .../arduino/sketchbook/libraries/ArduinoThread/Thread.cpp:9:18: error: cast from ‘Thread*’ to ‘int’ loses precision [-fpermissive] ThreadID = (int)this; ^~~~ --- README.md | 2 +- Thread.cpp | 2 +- Thread.h | 2 +- ThreadController.cpp | 2 +- ThreadController.h | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b866337..655d405 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,7 @@ interrupts(); // This will enable the interrupts egain. DO NOT FORGET! (Basically,the logic is: (reached time AND is enabled?). - `void Thread::onRun()` - The target callback function to be called. - `void Thread::run()` - Runs the thread (executes the callback function). -- `int Thread::ThreadID` - Theoretically, it's the memory address. It's unique, and can +- `uintptr_t Thread::ThreadID` - Theoretically, it's the memory address. It's unique, and can be used to compare if two threads are identical. - `int Thread::ThreadName` - A human-readable thread name. Default is "Thread ThreadID", eg.: "Thread 141515". diff --git a/Thread.cpp b/Thread.cpp index cd29d98..eadd1d1 100644 --- a/Thread.cpp +++ b/Thread.cpp @@ -6,7 +6,7 @@ Thread::Thread(void (*callback)(void), unsigned long _interval){ _cached_next_run = 0; last_run = millis(); - ThreadID = (int)this; + ThreadID = reinterpret_cast(this); #ifdef USE_THREAD_NAMES ThreadName = "Thread "; ThreadName = ThreadName + ThreadID; diff --git a/Thread.h b/Thread.h index 0e580a9..87b5088 100644 --- a/Thread.h +++ b/Thread.h @@ -61,7 +61,7 @@ class Thread{ bool enabled; // ID of the Thread (initialized from memory adr.) - int ThreadID; + uintptr_t ThreadID; #ifdef USE_THREAD_NAMES // Thread Name (used for better UI). diff --git a/ThreadController.cpp b/ThreadController.cpp index 7d8e41c..220edd6 100644 --- a/ThreadController.cpp +++ b/ThreadController.cpp @@ -63,7 +63,7 @@ bool ThreadController::add(Thread* _thread){ return false; } -void ThreadController::remove(int id){ +void ThreadController::remove(uintptr_t id){ // Find Threads with the id, and removes for(int i = 0; i < MAX_THREADS; i++){ if(thread[i]->ThreadID == id){ diff --git a/ThreadController.h b/ThreadController.h index 8e2888c..d13fa4d 100644 --- a/ThreadController.h +++ b/ThreadController.h @@ -36,7 +36,7 @@ class ThreadController: public Thread{ bool add(Thread* _thread); // remove the thread (given the Thread* or ThreadID) - void remove(int _id); + void remove(uintptr_t _id); void remove(Thread* _thread); // Removes all threads