kernel hang when not doing tasklet_kill - linux-kernel

I write simple code to test the function of tasklet.
When I don't do tasklet_kill, the kernel will be hang after the insmod command is using. Since there is no log, I have no idea what happens.
The following is my code.
void work_fcn(unsigned long a)
{
printk("this is tasklet work function\n");
}
void tasklet_test(void)
{
struct tasklet_struct task;
tasklet_init(&task, work_fcn, 0);
tasklet_schedule(&task);
//if I don't do the following line, then kernel hang
tasklet_kill(&task);
}
static int __init hello_init(void)
{
tasklet_test();
return 0;
}
module_init(hello_init);
Thanks.

static void tasklet_action_common(struct softirq_action *a,
struct tasklet_head *tl_head,
unsigned int softirq_nr)
{
...
while (list) {
struct tasklet_struct *t = list;
list = list->next;
if (tasklet_trylock(t)) {
if (!atomic_read(&t->count)) {
if (!test_and_clear_bit(TASKLET_STATE_SCHED,
&t->state)) //<===========(1)
BUG();
t->func(t->data);
tasklet_unlock(t);
continue;
}
tasklet_unlock(t);
}
...
}
}
In the comment (1), it checks if the tasklet status is TASKLET_STATE_SCHED, and if so, it will panic.
void tasklet_kill(struct tasklet_struct *t)
{
if (in_interrupt())
pr_notice("Attempt to kill tasklet from interrupt\n");
while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
do {
yield();
} while (test_bit(TASKLET_STATE_SCHED, &t->state));
}
tasklet_unlock_wait(t);
clear_bit(TASKLET_STATE_SCHED, &t->state); //<=========(2)
}
EXPORT_SYMBOL(tasklet_kill);
In the comment (2)will clear TASKLET_STATE_SCHED bit,that will not panic.

Related

std::list::remove_if goes crazy if combined with a generic lambda

I found a problem that I guess is due to a bug in GCC.
Anyway, before opening an issue, I would like to be sure.
Consider the code below:
#include<algorithm>
#include<list>
template<typename U>
struct S {
using FT = void(*)();
struct T { FT func; };
template<typename>
static void f() { }
std::list<T> l{ { &f<int> }, { &f<char> } };
void run() {
l.remove_if([](const T &t) { return t.func == &f<int>; }); // (1)
l.remove_if([](const auto &t) { return t.func == &f<int>; }); // (2)
}
};
int main() {
S<void> s;
s.run();
}
clang v3.9 compiles both (1) and (2) as expected.
GCC v6.2 compiles (1), but it doesn't compile (2).
The returned error is:
error: 'f' was not declared in this scope
Moreover, note that GCC compiles (2) if it is modified as it follows:
l.remove_if([](const auto &t) { return t.func == &S<U>::f<int>; }); // (2)
As far as I know, using an const auto & instead of const T & should not alter the behavior in this case.
Is it a bug of GCC?
Per [expr.prim.lambda]:
8 - [...] [For] purposes of name lookup (3.4) [...] the compound-statement is considered in the context of the lambda-expression. [...]
MCVE:
template<int>
struct S {
template<int> static void f();
S() { void(*g)(char) = [](auto) { f<0>; }; }
};
S<0> s;
Hoisting the compound-statement to the context of the lambda-expression gives a clearly valid program:
template<int>
struct S {
template<int> static void f();
S() { f<0>; }
};
S<0> s;
So yes, this is a bug in gcc.

c++ thread pool with mutable threads: strange deadlock when assigning tasks to threaads

hope you had all had nice holidays.
This questions is related to my earlier question: std::condition_variable - Wait for several threads to notify observer
I'm trying to implement a threadpool based on my own mutable thread implementation below:
class MutableThread
{
private:
std::thread m_Thread;
std::function<void()> m_Function;
bool m_bRun;
std::mutex m_LockMutex;
std::mutex m_WaitMutex;
std::condition_variable m_CV;
IAsyncTemplateObserver<MutableThread>* m_Observer = nullptr;
private:
void Execute()
{
while (m_bRun)
{
{
std::unique_lock<std::mutex> wait(m_WaitMutex);
m_CV.wait(wait);
}
std::lock_guard<std::mutex> lock(m_LockMutex);
if (m_bRun && m_Function)
{
m_Function();
m_Function = std::function<void()>();
if (m_Observer != nullptr)
{
m_Observer->Signal(this);
}
}
}
}
public:
HDEBUGNAME(TEXT("MutableThread"));
MutableThread(const MutableThread& thread) = delete;
MutableThread(IAsyncTemplateObserver<MutableThread>* _Observer)
{
m_Observer = _Observer;
m_bRun = true;
m_Thread = std::thread(&MutableThread::Execute, this);
}
MutableThread()
{
m_Observer = nullptr;
m_bRun = true;
m_Thread = std::thread(&MutableThread::Execute, this);
}
~MutableThread()
{
m_bRun = false;
m_CV.notify_one();
try
{
if (m_Thread.joinable())
m_Thread.join();
}
catch (std::system_error& ex)
{
HWARNINGD(TEXT("%s"), ex.what());
}
}
inline bool Start(const std::function<void()>& f)
{
std::lock_guard<std::mutex> lock(m_LockMutex);
if (m_Function != nullptr)
return false;
m_Function = f;
m_CV.notify_one();
return true;
}
The IAsyncTemplateObserver simply derives from my IAsyncObserver class posted in the earlier question and adds a virtual function:
template <typename T>
class IAsyncTemplateObserver : public IAsyncObserver
{
public:
virtual void Signal(T* _Obj) = 0;
};
What I want to do is, signal the ThreadPool that the function has finished execution and a new task is assigned to the mutable thread:
class MutableThread;
struct Task
{
std::function<void()> m_Function;
uint32_t m_uPriority;
Task(const std::function<void()>& _Function, uint32_t _uPriority)
{
m_Function = _Function;
m_uPriority = _uPriority;
}
};
inline bool operator<(const Task& lhs, const Task& rhs)
{
return lhs.m_uPriority < rhs.m_uPriority;
}
class ThreadPool : public IAsyncTemplateObserver<MutableThread>
{
private:
std::list<MutableThread* > m_FreeThreads;
std::list<MutableThread* > m_UsedThreads;
std::set<Task> m_Tasks;
std::mutex m_LockMutex;
public:
ThreadPool()
{
//Grow(std::thread::hardware_concurrency() - 1);
}
ThreadPool(size_t n)
{
Grow(n);
}
~ThreadPool()
{
//std::lock_guard<std::mutex> lock(m_Mutex);
for (MutableThread* pUsed : m_UsedThreads)
{
HSAFE_DELETE(pUsed);
}
for (MutableThread* pFree : m_FreeThreads)
{
HSAFE_DELETE(pFree);
}
}
inline void Grow(size_t n)
{
std::lock_guard<std::mutex> lock(m_LockMutex);
for (size_t i = 0; i < n; i++)
{
m_FreeThreads.push_back(new MutableThread(this));
}
}
inline void AddTask(const Task& _Task)
{
{
std::lock_guard<std::mutex> lock(m_LockMutex);
m_Tasks.insert(_Task);
}
AssignThreads();
}
virtual void Signal(MutableThread* _pThread)
{
{
std::lock_guard<std::mutex> lock(m_LockMutex);
m_UsedThreads.remove(_pThread);
m_FreeThreads.push_back(_pThread);
}
AssignThreads();
NotifyOne();
}
inline void WaitForAllThreads()
{
bool bWait = true;
do
{
{
//check if we have to wait
std::lock_guard<std::mutex> lock(m_LockMutex);
bWait = !m_UsedThreads.empty() || !m_Tasks.empty();
}
if (bWait)
{
std::unique_lock<std::mutex> wait(m_ObserverMutex);
m_ObserverCV.wait(wait);
}
} while (bWait);
}
private:
inline void AssignThreads()
{
std::lock_guard<std::mutex> lock(m_LockMutex);
if (m_FreeThreads.empty() || m_Tasks.empty())
return;
//Get free thread
MutableThread* pThread = m_FreeThreads.back();
m_FreeThreads.pop_back();
//park thread in used list
m_UsedThreads.push_back(pThread);
//get task with highest priority
std::set<Task>::iterator it = m_Tasks.end();
--it; //last entry has highest priority
//start the task
pThread->Start(it->m_Function);
//remove the task from the list
m_Tasks.erase(it);
}
The AddTask function is called several times by the same thread, but when a mutable thread signals the threadpool (via m_Observer->Signal(this) ) the application freezes at the lock_guard of the AssignThreads() function. Now the strange thing is unlike a normal deadlock, all callstack-views in Visual Studio are empty as soon is I try to step over the line with the lock_guard.
Can anyone explain this behaviour? Is there any major design flaw or just a simple mix up?
Thanks for your help!
Greetings,
Fabian
Edit: I've added a minimal visual studio solution that reproduces the problem: ThreadPoolTest.zip
Thanks to a friend, I was able to fix the problem by moving the call m_Observer->Signal(this) outside of the lock_guard scope in the MutableThread::Execute() function. Secondly I removed the lock_guard in the AssignThreads() function and moved its call into the scope of the lock_guard in the Signal()/AddTask function. Not really related but still a flaw: all condition_variables.wait() calls are now in a while(m_bNotified == false) loop.

boost asio post not working , io_service::run exits right after post

I am trying to mix boost signals with asio to do a dispatch based handler invocation. when the post method is invoked from a thread the io_service::run exits immediately, the callback handled to post is never invoked, callback is a C++11 lambda routine. I am pasting the code for more analysis.
#include<iostream>
#include<thread>
#include<boost/signals2/signal.hpp>
#include<boost/asio.hpp>
static boost::asio::io_service svc;
static boost::signals2::signal<void(std::string)> textEntered;
static void
handleInputText(std::string text)
{
std::cout<<"handleInputText()"<<" text provided: "<<text;
return;
}
static void
worker()
{
sleep(2);
svc.post([](){
std::cout<<"\nRaising signal.";
std::string hello("hello world");
textEntered(hello);
});
return;
}
int main(int ac, char **av)
{
try
{
textEntered.connect(&handleInputText);
std::thread w(std::bind(&worker));
svc.run();
w.join();
}
catch(std::exception &ex)
{
std::cerr<<"main() exited with exception:"<<ex.what();
}
return 0;
}
You don't actually post any work to the service.
You start a thread that may eventually post work, but the main thread has already exited by that time.
Either, run the ioservice on the thread or make sure it has io_service::work
Here's a fix with a dedicated service thread and a work item:
Live On Coliru
#include<boost/asio.hpp>
#include<iostream>
#include<boost/asio.hpp>
#include<boost/signals2.hpp>
#include<boost/thread.hpp>
#include<boost/make_shared.hpp>
static boost::asio::io_service svc;
static boost::shared_ptr<boost::asio::io_service::work> work_lock;
static boost::signals2::signal<void(std::string)> textEntered;
static void
handleInputText(std::string text)
{
std::cout<<"handleInputText()"<<" text provided: "<<text;
return;
}
static void
worker()
{
sleep(2);
svc.post([](){
std::cout<<"\nRaising signal.";
std::string hello("hello world");
textEntered(hello);
});
return;
}
int main()
{
try
{
work_lock = boost::make_shared<boost::asio::io_service::work>(svc);
textEntered.connect(&handleInputText);
boost::thread_group tg;
tg.create_thread(boost::bind(&boost::asio::io_service::run, &svc));
tg.create_thread(&worker);
boost::this_thread::sleep_for(boost::chrono::seconds(3));
work_lock.reset();
tg.join_all();
}
catch(std::exception &ex)
{
std::cerr<<"main() exited with exception:"<<ex.what();
}
return 0;
}
Prints:
Raising signal.handleInputText() text provided: hello world

Arduino turn on a led for 2 seconds and then turn off and wait 3 seconds and start all over again

I'm using a microcontroler PIC 32 in a diligent cerebot Mx4cK.
I have switch implemented on my protoboard and I want to turn on a led after the switch it's activated, then this led have to be in this state for 2 seconds and then have to be off for another 3 seconds and start all over again (on->2 seconds off->3 seconds)
This is my code so far, I think it's missing one condition but I can't find it... can you help me?
const int led=PIN_LED1;
const int pinSwitch1=16;
void setup()
{
pinMode(pinSwitch1,INPUT);
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
}
void loop()
{
unsigned long actual_time=millis();
static unsigned long cicle_time=0;
static unsigned long off_time=0;
static int switch_state1=0;
switch_state1=digitalRead(pinSwitch1);
if (switch_state1==HIGH)
{
if((actual_time-cicle_time)<5000)
{
digitalWrite(led,HIGH);
cicle_time=actual_time;
}
if((actual_time-off_time)>2000)
{
digitalWrite(led,LOW);
off_time=actual_time;
}
}
else
{
digitalWrite(led,LOW);
}
}
Actually my code, blincks for 2 seconds and it's not consider the 3 seconds that it has to be off.
[This is my new code, I missing an initial condition to light for the first time]
const int led=PIN_LED1;
const int pinSwitch1=16;
void setup()
{
pinMode(pinSwitch1,INPUT);
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
}
void loop()
{
unsigned long actual_time=millis();
static unsigned long cicle_time=0;
static unsigned long off_time=0;
static int switch_state1=0;
static int cicle_on=0;
switch_state1=digitalRead(pinSwitch1);
if (switch_state1==HIGH)
{
if((actual_time-cicle_time)>5000)
{
digitalWrite(led,HIGH);
cicle_time=actual_time;
cicle_on=HIGH;
}
}
else
{
digitalWrite(led,LOW);
}
if((actual_time-off_time)>2000)
{
digitalWrite(led,LOW);
off_time=actual_time;
cicle_on=LOW;
}
}
generic code debug is off-topic here
and you even do not specify what this does instead of what it should do
you are writing to LED every cycle
which slows things down
when you add more stuff then it could give you many head ages later
use absolute time instead of relative for your planned events and update only when needed
static unsigned long time_LED_on =0xFFFFFFFF;
static unsigned long time_LED_off=0xFFFFFFFF;
//...
if ((switch_state1==HIGH)&&(time_LED_on!=0xFFFFFFFF)) // init on switch toggle only would be better in interrupt
{
time_LED_on =actual_time;
time_LED_off=actual_time+2000;
}
if (switch_state1==LOW ) // stop if switch off also would be better in interrupt
{
time_LED_on =0xFFFFFFFF;
time_LED_off=0xFFFFFFFF;
}
// handle LED event
if (time_LED_on >=actual_time) { digitalWrite(led,HIGH); time_LED_on +=5000; }
if (time_LED_off>=actual_time) { digitalWrite(led,LOW ); time_LED_off+=5000; }
beware that time can overflow you can handle it by
if (min(all_times) > max) all_times -= max;
dont know how many bits your platform have if it is not 32 then change the 0xFFFFFFFF acordingly
This should solve your problem:
const int led=PIN_LED1;
const int pinSwitch1=16;
unsigned long cicle_time=0;
void setup()
{
pinMode(pinSwitch1,INPUT);
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
}
void loop()
{
unsigned long actual_time=0;
static int switch_state1=0
switch_state1=digitalRead(pinSwitch1);
digitalWrite(led,LOW);
while(switch_state1==HIGH)
{
digitalWrite(led,HIGH);
cicle_time=millis();
while((millis()-cicle_time)!>=2000)
{
}
cicle_time=millis();
digitalWrite(led,LOW);
while((millis()-circle_time)!>=3000)
{
}
}
}

CSingleLock implementation works ok on Windows 7 but ends in a deadlock on Win XP

I've tried substituting MFC's CSingleLock implementation with my own, but I now have a deadlock on windows XP which I don't have on windows 7 and which I did not have on either OSs with MFC's CSingleLock, besides looking into every Lock & Unlock in my app, what is missing from my implementation:
class CCriticalSection
{
CRITICAL_SECTION m_cs;
public:
CCriticalSection()
{
InitializeCriticalSection(&m_cs);
}
~CCriticalSection()
{
DeleteCriticalSection(&m_cs);
}
void Lock()
{
EnterCriticalSection(&m_cs);
}
BOOL TryLock()
{
return TryEnterCriticalSection(&m_cs);
}
void Unlock()
{
if(m_cs.LockCount > -1)
LeaveCriticalSection(&m_cs);
}
};
and
#include "CCriticalSection.h"
class CSingleLock {
CCriticalSection *m_cs;
public:
CSingleLock(CCriticalSection* cs = NULL, bool bLock = false)
{
m_cs = cs;
if(m_cs != NULL)
{
if(bLock)
m_cs->Lock();
}
}
void Unlock()
{
if(m_cs != NULL)
m_cs->Unlock();
}
void Lock()
{
if(m_cs != NULL)
m_cs->Lock();
}
~CSingleLock()
{
if(m_cs != NULL)
m_cs->Unlock();
}
};
To make the locking class really helpful, you need to improve it like this:
class CSingleLock
{
CCriticalSection *m_cs;
bool m_bLock;
public:
CSingleLock(CCriticalSection* cs = NULL, bool bLock = false)
{
m_cs = cs;
if(m_cs != NULL)
{
if(bLock)
m_cs->Lock();
m_bLock = bLock;
}
}
void Unlock()
{
if(!m_cs || !m_bLock)
return;
m_cs->Unlock();
m_bLock = false;
}
void Lock()
{
if(!m_cs || m_bLock)
return;
m_cs->Lock();
m_bLock = true;
}
~CSingleLock()
{
Unlock();
}
};
It should be intelligent enough to keep you away from trouble of incorrect CS use.
MSDN says "If a thread calls LeaveCriticalSection when it does not have ownership of the specified critical section object, an error occurs that may cause another thread using EnterCriticalSection to wait indefinitely.".
The destructor for CSingleLock calls CriticalSection::Unlock which calls LeaveCriticalSection but there's no check that the critical section is held by the current thread.
You need to keep track of ownership of the critical section.

Resources