Skip to content

Get rid of ATL dependency#4320

Closed
d0vgan wants to merge 1 commit intonotepad-plus-plus:masterfrom
d0vgan:feature/get-rid-of-atl-dependency
Closed

Get rid of ATL dependency#4320
d0vgan wants to merge 1 commit intonotepad-plus-plus:masterfrom
d0vgan:feature/get-rid-of-atl-dependency

Conversation

@d0vgan
Copy link
Copy Markdown
Contributor

@d0vgan d0vgan commented Mar 19, 2018

The ATL dependency is quite annoying when working in Visual Studio Express (where there's no ATL included). Moreover, currently it's easy to get rid of the ATL dependency at all - actually it does not add anything to the code that really worth this dependency.

@d0vgan d0vgan force-pushed the feature/get-rid-of-atl-dependency branch from cd9a25f to 91060c2 Compare March 21, 2018 11:51

#include <list>

class CCriticalSection
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A very intuitive class, with its implementation quite similar to ATL's CComAutoCriticalSection. (Actually, there is nothing "Com" in it.)

CRITICAL_SECTION m_cs;
};

class CCriticalSectionLock
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation is quite similar to ATL's CComCritSecLock.

@MAPJe71
Copy link
Copy Markdown
Contributor

MAPJe71 commented Mar 21, 2018

Why implement a new locking mechanism when there is already one available in .\src\MISC\common\mutex.*?

@d0vgan
Copy link
Copy Markdown
Contributor Author

d0vgan commented Mar 22, 2018

Agree, Yuni::Mutex could be used instead. In such case I'll add a new class Yuni::MutexLockerEx that will include the m_bLocked member and additional methods Lock and Unlock - similar to CComCritSecLock.

@MAPJe71
Copy link
Copy Markdown
Contributor

MAPJe71 commented Mar 22, 2018

There's already a Yuni::MutexLocker class. Why not use that one?

@d0vgan
Copy link
Copy Markdown
Contributor Author

d0vgan commented Mar 22, 2018

@MAPJe71, please look at the implementation of Yuni::MutexLocker and then re-read my previous comment :) This is because of the following line in CThreadSafeQueue::push :
lock.Unlock();
So it's kind of lock_guard with additional manual methods Lock and Unlock.

@MAPJe71
Copy link
Copy Markdown
Contributor

MAPJe71 commented Mar 22, 2018

Dispite using an autolocker/lock-guard on a mutex you can still manually (un-)lock it e.g.

template <typename C>
class CThreadSafeQueue : protected std::list<C>
{
public:

	void push(C& c)
	{
		Yuni::MutexLocker lock(m_Mutex); //<-- auto lock (RAII)
		push_back( c );
		m_Mutex.unlock(); //<-- manual unlock

		if (!::ReleaseSemaphore(m_hSemaphore, 1, NULL))
		{
			// If the semaphore is full, then take back the entry.
			m_Mutex.lock(); //<-- manual lock
			pop_back();
			if (GetLastError() == ERROR_TOO_MANY_POSTS)
			{
				m_bOverflow = true;
			}
		}
	} //<-- auto unlock

	bool pop(C& c)
	{
		Yuni::MutexLocker lock(m_Mutex);

        // 
	}

protected:
	Yuni::Mutex m_Mutex;
};

@d0vgan
Copy link
Copy Markdown
Contributor Author

d0vgan commented Mar 22, 2018

@MAPJe71,
I bet you've already noticed what's wrong with that code.
Surely, it's this situation:

{
    Yuni::MutexLocker lock(m_Mutex); //<-- auto lock (RAII)
    m_Mutex.unlock(); //<-- manual unlock
} //<-- auto unlock - shit, unlocked twice!

@MAPJe71
Copy link
Copy Markdown
Contributor

MAPJe71 commented Mar 22, 2018

Oooh, d#mn it you're right that won't work (as intended) 😁

@donho and @milipili could you extend the Yuni::MutexLocker class with manual un-/lock?
Would you consider a PR for libyuni?

@d0vgan
Copy link
Copy Markdown
Contributor Author

d0vgan commented Mar 22, 2018

I'd rather propose to have 2 classes: MutexLocker and MutexLockerEx, without inheritance.
Here is why:

  1. MutexLockerEx will have an additional bool member variable (see the proposed CCriticalSectionLock implementation), which is simply not needed for a lightweight MutexLocker.
  2. MutexLockerEx will have an additional argument in the constructor (see the proposed CCriticalSectionLock implementation), which is simply not needed for a lightweight MutexLocker.

@d0vgan
Copy link
Copy Markdown
Contributor Author

d0vgan commented Mar 23, 2018

OK, guys - no additional classes now. Just a little more thinking and a little bit more braces :)

@d0vgan d0vgan force-pushed the feature/get-rid-of-atl-dependency branch from 5a6175c to bcf3e7e Compare March 24, 2018 21:17
@d0vgan
Copy link
Copy Markdown
Contributor Author

d0vgan commented Mar 26, 2018

Now I believe it's good enough.

@chcg
Copy link
Copy Markdown
Contributor

chcg commented Mar 29, 2018

@d0vgan additional benefit: building with mingw would be possible again, because ATL is not available there, see #4145 and there #2478 with patches to avoid ATL, see chcg@463bdc5#diff-5b536d07367d39b369aea9582ff0fe9d

with
std::unique_lock<std::mutex>
as replacement for
CComCritSecLock<CComAutoCriticalSection>

@d0vgan
Copy link
Copy Markdown
Contributor Author

d0vgan commented Mar 29, 2018

Wow, so I'm not the first one to propose this!
As for std::mutex, it's better with regards to C++ standard, however it has some overhead comparing to Critical Section (which is used by Yuni::Mutex).
Anyway, both solutions will work.

@d0vgan d0vgan force-pushed the feature/get-rid-of-atl-dependency branch from 23325ef to 3261a6b Compare April 16, 2018 18:47
@d0vgan
Copy link
Copy Markdown
Contributor Author

d0vgan commented Apr 16, 2018

rebased + now everything in 1 commit

@d0vgan
Copy link
Copy Markdown
Contributor Author

d0vgan commented Apr 18, 2018

Come on, why these ATL things are still here?
"Entities are not to be multiplied without necessity"!

@d0vgan
Copy link
Copy Markdown
Contributor Author

d0vgan commented Apr 25, 2018

I used to think that it's me who is conservative and not fast - now I see I'm not alone :)

@d0vgan d0vgan force-pushed the feature/get-rid-of-atl-dependency branch from 3261a6b to 56b2f07 Compare April 25, 2018 07:53
@milipili
Copy link
Copy Markdown
Contributor

milipili commented Apr 27, 2018

@MAPJe71 From my point of view and from my experience, unlocking a mutex in the middle of the code where it is supposed to be managed by the scope is far from a good idea. Like deleting some resource managed by unique_ptr. For this reason, it is unlikely that an unlock mecanism comes in libyuni, better fix the code with proper raii (but feel free to bring some particular use-case).

Regarding libyuni/mutex, this is historical reason as std::mutex didn't exist before as you can guess. As of now, as the official minimum version for n++ is 2015 (if not mistaken, would be better to switch to 2017 from my point of view, there is a better support for multithread and brings multiple fixes), I would recommend to replace yuni/mutex by its stl counterpart as there is no added value.

That's said, it is right here to use yuni/mutex here, for code consistency. That replacement has nothing to do with this MR and should be another one.

@milipili
Copy link
Copy Markdown
Contributor

@d0vgan I would encourage you next time to use multiple commits as it changes different things.

@milipili
Copy link
Copy Markdown
Contributor

@MAPJe71 Where did you find the snippet you posted regarding CThreadSafeQueue? I am surely missing something here as I can't find the unlock()

@milipili
Copy link
Copy Markdown
Contributor

@donho For information, libyuni is going full C++17, so Yuni::Mutexshould disappear in the future (depending if users are still using it or not)

@MAPJe71
Copy link
Copy Markdown
Contributor

MAPJe71 commented Apr 27, 2018

@milipili the snippet is from PowerEditor/src/WinControls/ReadDirectoryChanges/ThreadSafeQueue.h.

@milipili
Copy link
Copy Markdown
Contributor

milipili commented Apr 27, 2018

Ah yes right. Inheriting from std::list.... Just kill it with fire. Twice :)

@milipili
Copy link
Copy Markdown
Contributor

@donho This class CThreadSafeQueue should die, or at least rewritten. No one must inherit from any stl container, as specified by the standard. Better use encapsulation instead of inheritance anyway.

@milipili
Copy link
Copy Markdown
Contributor

ATL dependency is really annoying. Nice job :)

@d0vgan
Copy link
Copy Markdown
Contributor Author

d0vgan commented Jul 8, 2018

After some thinking, I think I vote for chcg's patch:
chcg/notepad-plus-plus@463bdc5#diff-5b536d07367d39b369aea9582ff0fe9d
It uses the standard STL's std::mutex - and it is better than relying on thirdparty's Yuni::MutexLocker, which additionally needs to be consireder to be updated.

@MIvanchev
Copy link
Copy Markdown

@d0vgan: just to provide some aux. info, since I had the same fundamental problem as well. ATL/MFC is available with the Visual C++ Build Tools 2015. You can build Notepad++ easily that way.

@chcg
Copy link
Copy Markdown
Contributor

chcg commented Oct 25, 2018

@MIvanchev The main point to get ride of ATL I think is to be able to build this under mingw.

@MIvanchev
Copy link
Copy Markdown

@MIvanchev The main point to get ride of ATL I think is to be able to build this under mingw.

Yes, I also think the mingw build should be restored. I wanted to raise awareness that the Build Tools include ATL/MFC, because I wasted 2 days trying to figure out things... =)

@chcg chcg added build / code enhancement Proposed enhancements of existing features labels Aug 4, 2019
@donho donho self-assigned this Aug 24, 2019
@donho
Copy link
Copy Markdown
Member

donho commented Aug 24, 2019

@d0vgan Sorry for the late review. Could you solve the conflict please?
Also could you use lock_guard instead please?

@donho
Copy link
Copy Markdown
Member

donho commented Aug 24, 2019

@d0vgan Never mind, I will take in charge of it. Thank you for the PR.

@donho donho closed this in 3439071 Aug 25, 2019
iczelia pushed a commit to iczelia/notepad-plus-plus that referenced this pull request Jan 17, 2021
 Use std::lock_guard instead of CComCritSecLock<CComAutoCriticalSection>

 Close notepad-plus-plus#4320
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build / code enhancement Proposed enhancements of existing features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants